从 vue 子组件访问密钥 [英] Access key from child component in vue
问题描述
根据 Vue 文档,绑定在 v-for 中使用自定义组件需要一个键:
According to Vue docs, binding a key is required to use custom components in a v-for:
<template v-for="(task,i) in tasks">
<task-card v-bind:task="task" v-bind:key="i"></task-card>
</template>
我想在子组件 (task-card
) 中使用该键,但既不使用 this.key
也不添加 key
作为道具(是保留的 Vue 关键字)有效.有没有办法在不传递另一个值为i"的道具的情况下做到这一点?目前正在使用 "vue": "^2.5.9"
.
I would like to use that key in the child component (task-card
), but neither using this.key
or adding key
as a prop (is a reserved Vue keyword) works. Is there a way of doing this without passing yet another prop with the value "i"? Currently working with "vue": "^2.5.9"
.
推荐答案
如果你想将数据传递给孩子,你应该使用 props(key
是保留的,所以你必须命名它是别的东西).
If you want to pass data to the child, you should be using props (key
is reserved so you'll have to name it something else).
否则你可以通过this.$vnode.key
访问组件内vnode上的key.
Otherwise you can access the key on the vnode within the component via this.$vnode.key
.
这篇关于从 vue 子组件访问密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!