• vue列表渲染


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    
    <body>
    <div id="demo">
        <p>v-for遍历数组</p>
        <ul>
            <li v-for="(p,index) in persons" :key="index">
                {{p.name}} ---{{p.age}} ---{{index}} -----
                <button @click="del(index)">删除</button>
                <button @click="update(index,{name:'Cat',age:46})">更新</button>
            </li>
        </ul>
        <p>v-for遍历对象</p>
        <ui>
            <li v-for="(value,key) in persons[1]"  :key="key">
                {{key}}-----{{value}}
            </li>
        </ui>
    </div>
    <script type="text/javascript" src="lib/vue.min.js"></script>
    <script type="text/javascript">
        new Vue({
            el: "#demo",
            data: {
                persons: [ // vue只是监视了persons的改变, 没有监视persons内部数组的改变
                    {name: 'Tom', age: 12},
                    {name: 'Admin', age: 13},
                    {name: 'Root', age: 16},
                    {name: 'Rose', age: 10},
                ]
            },
            methods: {
                del(index){
                    //删除指定的元素
                    this.persons.splice(index, 1)   // 删除功能
                },
                update(index, newP){
                    // 没有调用vue 数组的变异方法
    //                this.persons[index] = newP;  //该操作没有改变persons对象,只是改变了person指向的数组中的元素对象
    
    
                    this.persons.splice(index, 1, newP);  // 修改功能,先将index这个元素删除,再将newP添加到这儿
    //                this.persons.splice(index, 0, newP);  // 增加
    
    
                }
            }
    
        });
    </script>
    </body>
    </html>

    vue数组中的增删改实现:

    this.persons.splice(index, 1)   // 删除index这个元素
    this.persons.splice(index, 0, newP);  // 添加元素newP
    this.persons.splice(index, 1, newP);  // 将index下标元素改成newP
    

      

  • 相关阅读:
    hdoj Last non-zero Digit in N! 【数论】
    spin_lock &amp; mutex_lock的差别?
    20140514,微软5月14日公布8个安全补丁
    教你用笔记本破解无线路由器password
    SSL工作原理
    MS-SQLSERVER中的MSDTC不可用解决方法
    grub2手动引导ubuntu
    用递归翻转一个栈 Reverse a stack using recursion
    腾讯面试
    AngularJS移动开发中的坑汇总
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/12389773.html
Copyright © 2020-2023  润新知