1

In my Home.vue component file I have a template in side which I have a component app-server-status which i am runing as a v-for loop but it is throwing warning and shows error in my file how can I solve that I have googled a lot but didn't found any thing helpful

 <template>
<div>
    <app-server-status v-for="server in 4"></app-server-status> //this line shows error

</div>


</template>
<script>
import serverStatus from '../Components/ServerStatus.vue'
export default{
components: {
    'app-server-status':serverStatus
}
}
</script>

The error it shows is -:

[vue/valid-v-for] Custom elements in iteration require 'v-bind:key' directives.eslint-plugin-vue

I have not installed eslint

2 Answers 2

1

I have updated your code here

<template>
<div>
    <app-server-status v-for="server in 4" v-bind:key="server"></app-server-status> //this line shows error

</div>


</template>
<script>
import serverStatus from '../Components/ServerStatus.vue'
export default{
components: {
    'app-server-status':serverStatus
}
}
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Is it compulsory to use v-bind:key="server" whenever I got stuck in something like this? can't we fix it with some installation?
To give Vue a hint so that it can track each node’s identity, and thus reuse and reorder existing elements, you need to provide a unique key attribute for each item. It is recommended to provide a key attribute with v-for
1

You are getting an error because you didn't bind keys for your loop elements ... this is completely ok if your loop is only for display purpose ( no mutations ) you can ignore the error however if you want to mutate one of the elements during render then you have to bind keys so Vue knows which exact element you are trying to mutate / delete .

 <app-server-status v-for="server in 4" :key="server"></app-server-status>

NB: it's not recommended to bind indexes.

It is recommended to provide a key attribute with v-for whenever possible, unless the iterated DOM content is simple, or you are intentionally relying on the default behavior for performance gains

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.