[英]Can not get the value of id with vue $refs?
I see the about vue $ refs document, the console to see is {}, I do not know how to get the value of id?我看到关于vue $ refs的文档,控制台看到的是{},不知道怎么获取id的值? There
{ }
is no object, how to get $ refs id of value,but I think this {}
not get to object.I try to use console.log(this.$refs.dataInfo.id)
is undefined
. { }
没有对象,如何获取 $ refs id 的值,但我认为这个{}
没有得到对象。我尝试使用console.log(this.$refs.dataInfo.id)
is undefined
。
javascript file: javascript文件:
console.log(this.$refs)
HTML file: HTML 文件:
<tab-item v-for="item in category" :id="item.id" ref="dataInfo">{{item.name}}</tab-item>
console.log(this.$refs.dataInfo.id)
is undefined
because you are trying to access an element's attribute through a reference to a component. console.log(this.$refs.dataInfo.id)
undefined
,因为您试图通过对组件的引用来访问元素的属性。 Based on your example object, you can access it like this: this.$refs.dataInfo[0]['$el'].id
根据您的示例对象,您可以像这样访问它:
this.$refs.dataInfo[0]['$el'].id
That's an odd way of doing it, I believe.我相信这是一种奇怪的做法。 The Vue way of doing it would be:
Vue 的做法是:
this.$emit('dataChange', data)
.this.$emit('dataChange', data)
。 And in the parent component add an event listener @dataChange="myMethod"
to the component.@dataChange="myMethod"
到组件。 Having a good understanding of the basic concepts such as how the data flows in the application is a must and should not be overlooked!对应用程序中的数据如何流动等基本概念有很好的理解是必须的,不容忽视!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.