我的组件数量取决于数组数量,因此当我向数组添加新项目时,它应该创建新组件。
创建新组件时,我想获得有关它的参考,这就是我产生误解的地方。最后添加的组件是undefined当我尝试获取它时。
但是,如果我想在一段时间后获得参考,它会起作用。我猜是因为异步,但是我不确定。
为什么会发生这种情况,以及是否有避免使用的方法setTimeout?
<div id="app">
<button @click="addNewComp">add new component</button>
<new-comp
v-for="compId in arr"
:ref="`components`"
:index="compId"
></new-comp>
</div>
<script type="text/x-template " id="compTemplate">
<h1> I am a component {{index}}</h1>
</script>
Run Code Online (Sandbox Code Playgroud)
Vue.component("newComp",{
template:"#compTemplate",
props:['index']
})
new Vue({
el:"#app",
data:{
arr:[1,2,3,4]
},
methods:{
addNewComp:function(){
let arr = this.arr;
let components = this.$refs.components;
arr.push(arr.length+1);
console.log("sync",components.length);
console.log("sync",components[components.length-1])
setTimeout(() => {
console.log("async",components.length);
console.log("async",components[components.length-1])
}, 1);
}
}
})
Run Code Online (Sandbox Code Playgroud)
acd*_*ior 10
refs, 和$refs, 不是反应性的。
如果您想获取更新后的值,您应该等到下一个渲染周期更新 DOM。
而不是setTimeout你应该使用Vue.nextTick():
new Vue({
el:"#app",
data:{
arr:[1,2,3,4]
},
methods:{
addNewComp:function(){
let arr = this.arr;
let components = this.$refs.components;
arr.push(arr.length+1);
console.log("sync",components.length);
console.log("sync",components[components.length-1])
Vue.nextTick(() => { // changed here
console.log("async",components.length);
console.log("async",components[components.length-1])
}); // changed here
}
}
})
Run Code Online (Sandbox Code Playgroud)
这不是“黑客”,这是正确的做法。来自官方 API 文档:
Vue.nextTick( [回调, 上下文] )
参数:
{Function} [callback]{Object} [context]用法:
推迟在下一个 DOM 更新周期后执行的回调。在您更改一些数据以等待 DOM 更新后立即使用它。
Run Code Online (Sandbox Code Playgroud)// modify data vm.msg = 'Hello' // DOM not updated yet Vue.nextTick(function () { // DOM updated }) // usage as a promise (2.1.0+, see note below) Vue.nextTick() .then(function () { // DOM updated })