You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
因为通过 this.$nextTick 注册的回调函数会被放到视图更新后的下一个事件循环执行、而 for 循环每次都是更改的 this 实例上的 a 属性的值、当 for 执行完后 this.a = 9在执行 this.$nextTick 注朋的回调函数的时候获取 this.a 的时候获取到的值也就是 9 了
Vue 相关面试题
1.下列代码会输出什么(字节)??
分析
因为通过
this.$nextTick
注册的回调函数会被放到视图更新后的下一个事件循环执行、而 for 循环每次都是更改的this
实例上的a
属性的值、当 for 执行完后this.a = 9
在执行this.$nextTick
注朋的回调函数的时候获取this.a
的时候获取到的值也就是 9 了2.页面渲染出来的值是什么?
答案
分析
涉及到两个知识点,一个是 vue 响应式原理,一个是
created 和 mounted 两个生命周期的区别。
1 .
Vue
无法检测property
的添加或移除。由于Vue
会在初始化实例时对property
执行getter/setter
转化,所以property
必须在data
对象上存在才能让 Vue 将它转换为响应式的。所以this.a.b
中的b
是非响应式的2 .又因为在
created
视图未渲染时直接对对象a
的属性b
赋值data
里面的初值会改变的,但是在mounted
里面更新this.a.b
的值的时候是非响应式的,所以视图不会更新。注意,Vue3
之后采用了proxy
方式,所以 Vue3 版本会渲染出 2The text was updated successfully, but these errors were encountered: