vue 单选框样式_作为一位Vue工程师,这些开发技巧你都会吗?

ede53bb38de11cf7730483ec3c1cbcc0.png来源 | http://www.cnblogs.com/chanwahfung/p/12543103.html

路由参数解耦

一般在组件内使用路由参数,大多数人会这样做:
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        }    }}
另外你还可以通过函数模式来返回 props
const router = new vueRouter({    routes: [{        path: '/user/:id',        component: User,        props: (route) => ({            id: route.query.id        })    }]})
文档: https://router.vuejs.org/zh/guide/essentials/passing-props.html

函数式组件

函数式组件是无状态,它无法实例化,没有任何的生命周期和方法。创建函数式组件也很简单,只需要在模板添加 functional 声明即可。一般适合只依赖于外部数据的变化而变化的组件,因其轻量,渲染性能也会有所提高。组件需要的一切都是通过 context 参数传递。它是一个上下文对象,具体属性查看 文档 。这里 props 是一个包含所有绑定属性的对象。函数式组件