Vue. How to get a value of a quot;keyquot; attribute in created element(Vue.如何获取“键的值?创建元素中的属性)
问题描述
我尝试创建一个组件并获取其在 axios 中使用的密钥.元素已创建,但我无法获得密钥.未定义
I try to create a components and get its key for using in axios. Elements created, but I can't get a key. It's undefined
<div class="container" id="root">
<paddock is="paddock-item" v-for="paddock in paddocks" :key="paddock.key" class="paddock">
</paddock>
</div>
<script>
var pItem = {
props: ['key'],
template: '<div :test="key"></div>',
created: function() {
console.log(key);
}
};
new Vue({
el: '#root',
components: {
'paddock-item': pItem
},
data: {
paddocks: [
{key: 1},
{key: 2},
{key: 3},
{key: 4}
]
}
})
</script>
我尝试了一些变体,但没有结果 - @key 为空.
I try some variants, but no result - @key was empty.
推荐答案
这个答案回答了如何将密钥传递给子组件的问题.如果您只想从子组件内部获取当前密钥,请使用投票最高的答案.<小时>key
是 Vue 中的 特殊属性.您必须将您的财产称为其他名称.
This answer answers the question of how you would pass the key to a child component. If you just want to get the current key from inside the child component, use the highest voted answer.
key
is a special attribute in Vue. You will have to call your property something else.
这里是使用 pkey
的替代方法.
Here is an alternative using pkey
instead.
console.clear()
var pItem = {
props: ['pkey'],
template: '<div :test="pkey"></div>',
created: function() {
console.log(this.pkey);
}
};
new Vue({
el: '#root',
components: {
'paddock-item': pItem
},
data: {
paddocks: [{
key: 1
},
{
key: 2
},
{
key: 3
},
{
key: 4
}
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<div class="container" id="root">
<paddock-item v-for="paddock in paddocks" :pkey="paddock.key" :key="paddock.key" class="paddock">
</paddock-item>
</div>
这篇关于Vue.如何获取“键"的值?创建元素中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vue.如何获取“键"的值?创建元素中的属性


基础教程推荐
- 检查 HTML5 拖放文件类型 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- npm start 错误与 create-react-app 2022-01-01