如何追踪变化
- 当你把一个普通的 JavaScript 对象传入 Vue 实例作为 data 选项,Vue 将遍历此对象所有的属性,并使用 Object.defineProperty 把这些属性全部转为 getter/setter。Object.defineProperty 是 ES5 中一个无法 shim 的特性,这也就是 Vue 不支持 IE8 以及更低版本浏览器的原因。
- 这些 getter/setter 对用户来说是不可见的,但是在内部它们让 Vue 能够追踪依赖,在属性被访问和修改时通知变更
- 每个组件实例都对应一个 watcher 实例,它会在组件渲染的过程中把“接触”过的数据属性记录为依赖。之后当依赖项的 setter 触发时,会通知 watcher,从而使它关联的组件重新渲染。
检测变化的注意事项
- 受现代 JavaScript 的限制 (而且 Object.observe 也已经被废弃),Vue 无法检测到对象属性的添加或删除。由于 Vue 会在初始化实例时对属性执行 getter/setter 转化,所以属性必须在 data 对象上存在才能让 Vue 将它转换为响应式的。例如:
var vm = new Vue({
data:{
a:1
}
})
// `vm.a` 是响应式的
vm.b = 2
// `vm.b` 是非响应式的
- 对于已经创建的实例,Vue 不允许动态添加根级别的响应式属性。但是,可以使用 Vue.set(object, key, value) 方法向嵌套对象添加响应式属性。
Vue.set(this.someObject,'b',2);
//另一种写法
this.$set(this.someObject,'b',2);
如果需要向已有对象赋值多个新属性,使用Object.assign()或 _.extend()。但是,这样添加到对象上的新属性不会触发更新。在这种情况下,你应该用原对象与要混合进去的对象的属性一起创建一个新的对象。
this.someObject = Object.assign({},this.someObjcect,{a:1,b:2});
声明响应式属性
由于 Vue 不允许动态添加根级响应式属性,所以你必须在初始化实例前声明所有根级响应式属性
var vm = new Vue({
data: {
// 声明 message 为一个空值字符串
message: ''
},
template: '<div>{{ message }}</div>'
})
// 之后设置 `message`
vm.message = 'Hello!'
如果未在data中声明message,Vue 将警告你渲染函数正在试图访问不存在的属性。
异步更新队列
- Vue 在更新 DOM 时是异步执行的。只要侦听到数据变化,Vue 将开启一个队列,并缓冲在同一事件循环中发生的所有数据变更。如果同一个 watcher 被多次触发,只会被推入到队列中一次。这种在缓冲时去除重复数据对于避免不必要的计算和 DOM 操作是非常重要的。然后,在下一个的事件循环“tick”中,Vue 刷新队列并执行实际 (已去重的) 工作。
- Vue 在内部对异步队列尝试使用原生的 Promise.then 和 MessageChannel,如果执行环境不支持,则会采用 setTimeout(fn, 0) 代替。
如果想要在数据变化之后等待Vue完成更新Dom
可以在数据变化之后立即使用 Vue.nextTick(callback)。这样回调函数将在 DOM 更新完成后被调用。
<div id="app">
<p>{{message}}</p>
</div>
<script src="vue.min.js"></script>
<script>
var vm = new Vue({
el:'#app',
data(){
return{
message:'123'
}
}
})
vm.message = 'new Message';
console.log(vm.$el.textContent === 'new Message'); //false
Vue.nextTick(() => {
console.log(vm.$el.textContent === 'new Message'); //true
})
</script>
在组件内使用 vm.$nextTick() 实例方法特别方便,因为它不需要全局 Vue,并且回调函数中的 this 将自动绑定到当前的 Vue 实例上
new Vue({
el:'#app',
template:`
<div>
<p>{{message}}</p>
<button @click = 'updateMessage()'></button>
</div>
`,
data(){
return{
message:'未更新'
}
},
methods:{
updateMessage(){
this.message = '已更新';
// console.log(this.$el.data.message);
console.log(this.$el.textContent);
this.$nextTick(() => {
console.log(this.$el.textContent);
})
}
}
})
this.$nextTick(() => { }) 用于当你想基于更新后的 DOM 状态来做点什么
Object.assign()方法
- Object.assign(target,source);
- Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
<script>
const target = {a:1,b:2};
const source = {b:4,c:6};
const returnTarget = Object.assign(target,source);
console.log(target); //Object{a:1,b:4,c:6};
console.log(returnTarget);//Object{a:1,b:4,c:6};
</script>
由上述例子可知:
- 使用Object.assign()会改变目标对象的值
- 如果目标对象中的属性具有相同的键,则属性将被源对象中的属性覆盖。后面的源对象的属性将类似地覆盖前面的源对象的属性。
继承属性和不可枚举属性是不能拷贝的
const obj = Object.create({foo:1}, //foo是个继承属性
{
bar:{ //bar是个不可枚举属性
value:2
},
far:{
value:3,
enumerable:true //far是个自身可枚举属性
}
})
const copy = Object.assign({},obj);
console.log(copy); //{far:3}
Object.create()方法
- Object.create(proto, [propertiesObject])
- Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。
使用Object.create实现类式继承
function Shape(){
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function(x,y){
this.x += x;
this.y += y;
console.log('Shape moved');
}
//Rectangle 子类
function Rectangle(){
Shape.call(this);
}
Rectangle.prototype = Object(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rec = new Rectangle();
console.log(rec instanceof Rectangle); //true
console.log(rec instanceof Shape); //true
继承多个对象,可以采用混入的形式
// 继承一个类
MyClass.prototype = Object.create(SuperClass.prototype);
// 混合其它
Object.assign(MyClass.prototype, OtherSuperClass.prototype);
// 重新指定constructor
MyClass.prototype.constructor = MyClass;