A small question about v-for on a < template >

look at the official documentation that when using v-for, you need to use a unique key value to improve performance, saying that this is related to virtual dom.
but when I bind v-for to the template tag for looping, the binding key value shows an error, but the page outputs data normally

  • < template > cannot be keyed. Place the key on real elements instead.
I probably know the meaning of

, which means that key should be on top of the real element.
does the v-for in this case not have to bind the key value?
Code:

< html lang= "en" >
< head >

<meta charset="UTF-8">
<title>vuetest.html</title>
<link rel="stylesheet" href="./main.css">
<script src="./vue.js"></script>

< / head >
< body >

<div id="app">
    <template v-for="(item,index) in list" :key="item.id">
        

{{item.content}}

<span>{{item.id}}</span> </template> </div> <script> var vm = new Vue({ el:"-sharpapp", data:{ list:[ { id:0, content:"" }, { id:1, content:" " }, { id:2, content:"4" },{ id:3, content:"" } ], } }) </script>

< / body >
< / html >

clipboard.png

Apr.07,2021

using key does not improve performance, in order to prevent the default element reuse of Vue from causing data binding rendering problems in some cases, while template is a virtual node and there is no reuse. If you want to bind, just bind to the following p and span tags. Generally, you only need to bind the key value of the elements related to data binding


this is an anti-human design. V-for needs to: But you can't add: key, to template, then the loop must be placed on the html tag, not on template, to generate a pile of junk code, but if v-for is used on template, do not add: key, can ignore the warning, or turn off eslint

Menu