6

I'm trying to use a random string (UUID v4) with vue-uuid for current items and items added to the list in the future (this is a to-do list type app) but I'm not sure what to correct syntax is.

I installed it and added it to my project in main.js:

import UUID from 'vue-uuid';
Vue.use(UUID);

However, I don't know how to use it in my Vue component. This is what I tried:

Template:

<transition-group
  name="list"
  enter-active-class="animated bounceInUp"
  leave-active-class="animated bounceOutDown"
>
  <li v-for="item in skills" :key="uuid">{{ item.skill }}</li>
</transition-group>

Script:

import { uuid } from 'vue-uuid';

export default {
  name: 'Skills',
  data() {
    return {
      uuid: uuid.v4(),
      skill: '',
      skills: [{ skill: 'Vue.js' }, { skill: 'React' }]
    };
  },
};

For :key="uuid", I get an error saying Expected 'v-bind:key' directive to use the variables which are defined by the 'v-for' directive (vue/valid-v-for). I also tried changing it to :key="item.uuid" which makes that error go away, but then the list doesn't appear.

project repo (based on this Udemy Vue crash course)

nCardot
  • 5,992
  • 6
  • 47
  • 83
  • Check [this](https://stackoverflow.com/questions/53572459/how-can-i-generate-unique-ids-in-vue-js-from-attributes-in-a-json-file) – ravibagul91 Jan 18 '20 at 02:45
  • 1
    It's not a good practice, but if array don't have unique value to be used as a key then you can use the `index`. For example, `
  • `
  • – ravibagul91 Jan 18 '20 at 02:50
  • That's what my code was originally but I wanted to use a unique ID. – nCardot Jan 18 '20 at 02:59