【发布时间】:2018-03-13 13:02:44
【问题描述】:
我正在尝试使用 $refs 功能通过 Vue 访问 DOM 元素,但我无法让它工作。
我的元素如下所示。 plateId 是动态生成的,所以不会一直是同一个数字:
<textarea :ref="plateId + '-notes'">
我的 Vue 函数如下所示:
/* This does not work */
addNotes: function(plateId) {
console.log(this.$refs.plateId + '-notes');
}
每当我运行此代码并激活该功能时,它只会在我的控制台中读取undefined。我也试过这个,它也不起作用并且读取未定义:
/* This does not work */
addNotes: function(plateId) {
var plateIdNotes = plateId + '-notes';
console.log(this.$refs.plateIdNotes);
}
用const替换var(我正在使用ES6并转译代码)也不起作用:
/* This does not work */
addNotes: function(plateId) {
const plateIdNotes = plateId + '-notes';
console.log(this.$refs.plateIdNotes);
}
我知道ref 正确绑定到元素,因为当我在下面执行此操作时,我可以在控制台中看到我的所有其他参考,以及 plateId-notes 参考:
/* This works */
addNotes: function(plateId) {
console.log(this.$refs);
}
如何使用函数中的参数访问plateId ref?
【问题讨论】:
标签: javascript function parameters vue.js ref