小编Naz*_*uba的帖子

如何在Vue中动态创建的组件上获取更新的$ ref?

我的组件数量取决于数组数量,因此当我向数组添加新项目时,它应该创建新组件。

创建新组件时,我想获得有关它的参考,这就是我产生误解的地方。最后添加的组件是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)

码笔链接

javascript vue.js

5
推荐指数
1
解决办法
2366
查看次数

标签 统计

javascript ×1

vue.js ×1