专栏名称: 程序员大咖
程序员大咖,努力成就期待着的自己。分享程序员技术文章、程序员工具资源、程序员精选课程、程序员视频教程、程序员热点资讯、程序员学习资料等。
今天看啥  ›  专栏  ›  程序员大咖

8 个很棒的 Vue 开发技巧

程序员大咖  · 公众号  ·  · 2024-11-21 10:24
    

文章预览

1.路由参数解耦 通常在组件中使用路由参数,大多数人会做以下事情。 export   default  {      methods : {          getParamsId () {             return this.$route.params.id         }     } } 在组件中使用 $route 会导致与其相应路由的高度耦合,通过将其限制为某些 URL 来限制组件的灵活性。正确的做法是通过 props 来解耦。 const router = new VueRouter({ routes: [{ path: /user/:id , component: User, props: true }] }) 将路由的 props 属性设置为 true 后,组件内部可以通过 props 接收 params 参数。 export   default  {      props : [ id ],     methods: {          getParamsId () {             return this.id         }     } } 您还可以通过功能模式返回道具。 const router = new VueRouter({ routes: [{ path: /user/:id , component: User, props: (ro ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照
总结与预览地址:访问总结与预览