小编Kay*_*lfe的帖子

Vuejs动态添加ref未定义

我正在尝试创建一个小型数据编辑器。单击处理程序将在父类别中添加子项目,如下所示:

methods: {
  addBlank: function(parentKey){
  var obj = {}
  obj.id = Math.floor((Math.random() * 99999) + 1)
  obj.name = "New Item"
  obj.data1 = 9
  obj.data2 = 9
  obj.data3 = 9

  this.categories[0].children.push(obj)

  console.log("New ref is:",obj.id)
  console.log(this.$refs)  // new $ref is available
  console.log("I can't select it",this.$refs[obj.id]) //returns undefined
  console.log("But I can select others",this.$refs['4214234']) //this is ok
  }
}
Run Code Online (Sandbox Code Playgroud)

Codepen 示例: https: //codepen.io/Kay_Wolfe/pen/xJEVRW

当它存在时,为什么this.$refs[obj.id]回报不足?

javascript ref vue.js vuejs2

3
推荐指数
1
解决办法
2395
查看次数

标签 统计

javascript ×1

ref ×1

vue.js ×1

vuejs2 ×1