Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue. How to get a value of a "key" attribute in created element

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> 

I try some variants, but no result - @key was empty.

like image 945
McLotos Avatar asked Aug 24 '17 02:08

McLotos


People also ask

How can I get data value in Vuejs?

we will write button click event and get custom attribute value in vue. js. In this example we will take on button with custom attribute like "data-id" and on click event of button we will get that value of custom data attribute value in console. we will get data attribute value using event.

Why we use key in Vue JS?

key # The key special attribute is primarily used as a hint for Vue's virtual DOM algorithm to identify vnodes when diffing the new list of nodes against the old list. Without keys, Vue uses an algorithm that minimizes element movement and tries to patch/reuse elements of the same type in-place as much as possible.

What is the use of key attribute?

The Key attribute is used to denote the property that uniquely identifies an entity (the EntityKey ), and which is mapped to the Primary Key field in a database: public class Order. { [Key]

What is key in Vue component?

The key attribute gives Vue hints You, as the developer, need to give Vue some hints every now and then. The key attribute tells Vue how your data relates to the HTML elements it's rendering to the screen.


2 Answers

you don't need to use an extra attribute. You can get the key by

this.$vnode.key 
like image 126
Philipp Mochine Avatar answered Oct 18 '22 10:10

Philipp Mochine


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.

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>
like image 27
Bert Avatar answered Oct 18 '22 09:10

Bert