简体   繁体   English

用 vue $refs 无法获取 id 的值?

[英]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

Look at the picture:看图: 在此处输入图像描述

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 的做法是:

  1. Using events .使用事件 When props change in the child component and you want to do something about it in the parent component, you should emit a custom event.当子组件中的道具发生变化并且您想在父组件中对其进行处理时,您应该发出一个自定义事件。 You'd do something like this in the child component: this.$emit('dataChange', data) .你会在子组件中做这样的事情: this.$emit('dataChange', data) And in the parent component add an event listener @dataChange="myMethod" to the component.并在父组件中添加一个事件监听器@dataChange="myMethod"到组件。 Vue.js 中的父子组件关系
  2. Using Vuex .使用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!对应用程序中的数据如何流动等基本概念有很好的理解是必须的,不容忽视!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM