vue的双向绑定是基于响应式来作的,即给一个Vue对象的属性加上getter, setter
方法,在这些方法中处理双向绑定。但这种方式就会出现下面这些坑vue
举个例子,我写了个svg-icon的基础组件,相似以下:数组
<template> <svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" fit :style="style"> <g><path :d="path"/></g> </svg> </template> <script type="text/babel"> import propsMixin from './props-mixin' export default { mixins: [propsMixin], props: { path: { type: String, default: '' } }, computed: { style() { return { fill: 'currentcolor', verticalAlign: 'middle', width: this.size, height: this.size } } } } </script>
而后我每一个icon都只须要传入不一样的路径就能够了,babel
<template> <svg-icon path="M15.41 7.41l-1.41-1.41-6 6 6 6 1.41-1.41-4.58-4.59z" :size="size"></svg-icon> </template> <script> import svgIcon from './svg-icon.vue' import propsMixin from './props-mixin' export default { mixins: [propsMixin], components: { svgIcon } } </script>
然而这里就有个问题了,这里有个size属性决定icon的大小,若是我用这种方式来写,那么我每一个icon里面都须要声明size这个props,而且在模板上声明,我嘞个去。。。svg
Vue中的属性若是是Object,或者是数组,数组中有Object,那么这些Object最好在最开始就把全部须要用到的属性都定义一遍,若是在运行中从新添加属性,这个属性并非响应式的,就不会实现双向绑定,例如:组件化
const vm = new Vue({ data: { a: { text: 'aaa' } } }) vm.a.b = 'ccc'
这样的状况,a的b属性不是响应式的,因此不会双向绑定this
对Date对象的操做不是响应式的双向绑定