本篇文章整理分享8 個(gè)很棒的 Vue 開發(fā)技巧,包括路由參數(shù)解耦、功能組件、樣式范圍、watch的高級使用、watch監(jiān)聽多個(gè)變量等,希望對大家有所幫助!
1.路由參數(shù)解耦
通常在組件中使用路由參數(shù),大多數(shù)人會做以下事情。
export default { methods: { getParamsId() { return this.$route.params.id } } }
登錄后復(fù)制
在組件中使用 $route 會導(dǎo)致與其相應(yīng)路由的高度耦合,通過將其限制為某些 URL 來限制組件的靈活性。正確的做法是通過 props 來解耦。
const router = new VueRouter({ routes: [{ path: /user/:id , component: User, props: true }] })
登錄后復(fù)制
將路由的 props 屬性設(shè)置為 true 后,組件內(nèi)部可以通過 props 接收 params 參數(shù)?!?/p>