【发布时间】:2018-12-26 15:33:50
【问题描述】:
我正在尝试创建一个小型数据编辑器。单击处理程序将在父类别中添加一个子项,如下所示:
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
}
}
Codepen 示例:https://codepen.io/Kay_Wolfe/pen/xJEVRW
this.$refs[obj.id] 在那里的时候怎么会返回不足?
【问题讨论】:
-
既然
Math.ceil((Math.random() * 99999)可以实现同样的功能,为什么还要使用Math.floor((Math.random() * 99999) + 1)?但更多的是你的问题,你怎么知道this.$refs[obj.id]被定义了?显然不是。当您检查this.$refs时会显示什么? -
您需要等到使用
nextTick实际创建了引用。这是你的笔修好了。 codepen.io/Kradek/pen/bjweMa?editors=1010
标签: javascript vue.js vuejs2 ref