不用组件,也做了一个。但是,有什么问题呢?

<!DOCTYPE html>
<body>
<div id="app">
	<div v-for="(list, index) in lists" :key="list.id">
		{
    
    {
    
     list.name }}
		<button v-on:click="deleteIetm">删除</button></div>
	
</div>

<script src="https://unpkg.com/vue@next"></script>
<script>
 const app = Vue.createApp({
    
    
   data() {
    
    
     return {
    
    
    lists: [
      {
    
    id: 1, name: 'k1'},
      {
    
    id: 2, name: 'k2'},
      {
    
    id: 3, name: 'k3'},
    ],
    
     }
   },
   methods: {
    
    
     deleteIetm(index) {
    
    
       this.lists.splice(index, 1)
     }
     
    },
    
})
 
 app.mount('#app')

  </script>
</body>
</html>

这个不能自己点按键添加一行吧,只能手动添加。

没有发现用list.id和index有什么区别
https://www.cnblogs.com/youhong/p/11327062.html

<!DOCTYPE html>
<body>
<div id="app">
	<div v-for="(list, index) in lists" :key="index">
		{
    
    {
    
     list.name }}
		<button v-on:click="deleteIetm">删除</button></div>
	
</div>

<script src="https://unpkg.com/vue@next"></script>
<script>
 const app = Vue.createApp({
    
    
   data() {
    
    
     return {
    
    
    lists: [
      {
    
    id: 1, name: 'k1'},
      {
    
    id: 2, name: 'k2'},
	  {
    
    id: 3, name: 'k3'},
	  {
    
    id: 4, name: 'k4'},
    ],
    
     }
   },
   methods: {
    
    
     deleteIetm(index) {
    
    
       this.lists.splice(index, 1)
     }
     
    },
    
})
 
 app.mount('#app')

  </script>
</body>
</html>

:key="index"的情况
还有index换成list.id的情况
不明所以。

猜你喜欢

转载自blog.csdn.net/weixin_40945354/article/details/120177287