javascript - Can not get the value of id with vue $refs? - Stack Overflow

I see the about vue $ refs document, the console to see is {}, I do not know how to get the value of id

I see the about vue $ refs document, the console to see is {}, I do not know how to get the value of 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.

Look at the picture:

javascript file:

console.log(this.$refs)

HTML file:

<tab-item v-for="item in category" :id="item.id" ref="dataInfo">{{item.name}}</tab-item>

I see the about vue $ refs document, the console to see is {}, I do not know how to get the value of 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.

Look at the picture:

javascript file:

console.log(this.$refs)

HTML file:

<tab-item v-for="item in category" :id="item.id" ref="dataInfo">{{item.name}}</tab-item>
Share Improve this question edited Jan 21, 2018 at 5:58 wen tian asked Jan 21, 2018 at 5:07 wen tianwen tian 4412 gold badges6 silver badges18 bronze badges 1
  • 1 for one, it's all in {}.branid not just {} as you can see in the object tree, it starts off with {} but the item is branid which contains another item, id. – user4616966 Commented Jan 21, 2018 at 5:13
Add a ment  | 

1 Answer 1

Reset to default 3

console.log(this.$refs.dataInfo.id) is undefined because you are trying to access an element's attribute through a reference to a ponent. Based on your example object, you can access it like this: this.$refs.dataInfo[0]['$el'].id

That's an odd way of doing it, I believe. The Vue way of doing it would be:

  1. Using events. When props change in the child ponent and you want to do something about it in the parent ponent, you should emit a custom event. You'd do something like this in the child ponent: this.$emit('dataChange', data). And in the parent ponent add an event listener @dataChange="myMethod" to the ponent.
  2. Using Vuex.

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744407905a4572724.html

相关推荐

  • javascript - Can not get the value of id with vue $refs? - Stack Overflow

    I see the about vue $ refs document, the console to see is {}, I do not know how to get the value of id

    6天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信