您当前的位置: 首页 > 

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Vue——列表渲染1

小志的博客 发布时间:2019-09-01 22:47:35 ,浏览量:1

1、代码如下:




  
  列表渲染






  测试: v-for 遍历数组
  
    
      {{index}}--{{p.name}}--{{p.age}}
      --删除
      --更新
    
  
  添加

  测试: v-for 遍历对象

  
    {{key}}={{item}}
  





  new Vue({
    el: '#demo',
    data: {
      persons: [
        {name: 'Tom', age:18},
        {name: 'Jack', age:17},
        {name: 'Bob', age:19},
        {name: 'Mary', age:16}
      ]
    },

    methods: {
      deleteP (index) {
        this.persons.splice(index, 1) // 调用了不是原生数组的splice(), 而是一个变异(重写)方法
              // 1. 调用原生的数组的对应方法
              // 2. 更新界面
      },

      updateP (index, newP) {
        console.log('updateP', index, newP)
        // this.persons[index] = newP  // vue根本就不知道
        this.persons.splice(index, 1, newP)
        // this.persons = []
      },

      addP (newP) {
        this.persons.push(newP)
      }
    }
  })



2、效果图如下: 1)初始页面效果图: 在这里插入图片描述2)点击删除按钮效果图: 在这里插入图片描述3)点击更新按钮效果图: 在这里插入图片描述 4)点击添加按钮效果图: 在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0395s