1. 程式人生 > >vue學習(九) set用法

vue學習(九) set用法

1,示例程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="app">
        <div v-for="(item,index) of list" :key="item.id">
            {{item.id}}
        </div>


        <div v-for="(item,key,index) of userInfo">
            {{item}}  --- {{key}}  ---{{index}}
        </div>

    </div>

    <script>
        //Vue.set(app.list,1,{id:'003',name:'hhhh'})
        //app.$set(app.list,0,{id:'004',name:'ahshhs'})
        //Vue.set(app.userInfo,'sex','男')
        //app.$set(app.userInfo,'phone','12345678')
        var app=new Vue({
            el:"#app",
            data:{
                content:'hello vue',
                list:[{
                    id:"001",
                    name:'zhangsan'
                },{
                    id:'002',
                    name:'lisi'
                }],
                userInfo:{
                    id:'123',
                    name:'zhangsnah',
                    age:29,
                    address:'shanghai'
                }
            }
        })
    </script>
</body>
</html>

2,在控制檯輸入

Vue.set(app.list,1,{id:'003',name:'hhhh'})

//app.$set(app.list,0,{id:'004',name:'ahshhs'})

Vue.set(app.userInfo,'sex','男')   

//app.$set(app.userInfo,'phone','12345678')

頁面會及時更新

3,運算元組有7中方法

push  pop shift unshift  splice  sort reverse