首页 > 其他 > 详细

vue 列表渲染

时间:2020-04-12 11:35:24      阅读:46      评论:0      收藏:0      [点我收藏+]

v-for  遍历数组/v-for遍历对象

<template>
  <div>
    <h2>测试:v-for 遍历数组</h2>
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
          <th>号码</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(pp,index) in persons" :key="index">
          <td>{{pp.name}}</td>
          <td>{{pp.age}}</td>
          <td>{{pp.phone}}</td>
          <td>
            <button @click="deleteP(index)">删除</button>
            <button @click="edit(index,{name:‘cat‘,age:23,phone:15896589635})">修改</button>
          </td>
        </tr>
      </tbody>
    </table>
    <h2>测试:v-for 遍历对象</h2>
    <ul>
        <li v-for="(value,key) in persons[1]" :key="key">
            {{value}}---{{key}}
        </li>
    </ul>
  </div>
</template>
<script>
export default {
  data() {
    return {
      persons: [
        {
          name: "tom",
          age: 12,
          phone: 15926558969
        },
        {
          name: "jack",
          age: 23,
          phone: 15926558969
        },
        {
          name: "daata",
          age: 33,
          phone: 15926558969
        },
        {
          name: "huawei",
          age: 56,
          phone: 15926558969
        }
      ]
    };
  },
  methods: {
    deleteP(index) {
      this.persons.splice(index, 1);
    },
    edit(index, newP) {
      //   并没有改变persons本身,数组内部发生了变化,但并没有调用变异方法,vue不会更新界面
      //this.persons[index]=newP
      this.persons.splice(index,1,newP)
    }
  }
};
</script>
<style lang="less">
</style>

技术分享图片

 

 

vue本身只监视了数组的改变,没有监视数组内部的数据改变

vue重写了数组中的一系列改变数组内部数据的方法(先调用原生,更新界面)--数组内部改变,界面发生变化

Vue的数组更新

Vue 包含一组观察数组的变异的方法,所以他们也将会触发视图的更新。

push()  pop() shift() unshift() splice() sort() reverse()

 技术分享图片

 

vue 列表渲染

原文:https://www.cnblogs.com/recommencer/p/12684066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!