Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to bind on a source property in v-for

given this tutorial

as shown below in the code, i am trying to iterate through items array and display item.message
at run time, i receive the following error:

Elements in iteration expect to have 'v-bind:key' directives  vue/require-v-for-key

please let me know how can i bind on source property in v-for

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

code:

<template>
  <li v-for="item in items">
    {{ item.message }}
  </li>
</template>

<script>

export default {
  name: 'App',
}
</script>

<script setup>
import {ref} from 'vue' 

const items = ref([{ message: 'Foo' }, { message: 'Bar' }])

</script>

>Solution :

Read about :key in the Vue docs:
https://vuejs.org/guide/essentials/list.html#maintaining-state-with-key
To make your life easier just always provide :key for v-for by selecting some unique value in your objects in the list, in your case it seems message since you don’t have any id property:

<template>
  <li v-for="item in items" :key="item.message">
    {{ item.message }}
  </li>
</template>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading