vue之列表渲染(v-for)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="demo">
  <h2>测试:v-for遍历数组</h2>
  <ul>
    <li v-for="(p,index) in persons" :key="index">
     {{index}}---{{p.name}}----{{p.age}}
      ---<button @click="deletePerson(index)">删除</button>
      ---<button @click="updateP(index,{name:'Cat',age:20})">更新</button>
    </li>
  </ul>
  <h2>测试:v-for遍历对象</h2>
  <ul>
    <li v-for="(value,key) in persons[1]" :key="key">
      {{value}}----{{key}}
    </li>
  </ul>
</div>
<script src="../js/vue.js"></script>
<script>
  new Vue({
    el: '#demo',
    data:{
      persons:[//vue本身只是监视了persons的改变,没有监视数组内部数据的变化
        {name:"Tom",age:18},
        {name:"Tom1",age:181},
        {name:"Tom2",age:182},
        {name:"Tom3",age:183},
      ]
    },
    methods:{
      deletePerson(index){
        this.persons.splice(index,1)
      },
      updateP(index,newP){
        // this.persons[index]=newP   //数组内部虽然变化了,但是并没有调用变异方法
        this.persons.splice(index,1,newP)
      }
    }
  })
</script>
</body>
</html>
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/articles/14999998.html
                    
                
                
            
        
浙公网安备 33010602011771号