vue 绑定数组里面对象变化没法更新view

htmljavascript

 <div v-for="(content, index) in contents" @click="chnageVal(index)">
            <p>{{content.name}}</p>
            <div v-for="val in content.values">
                <p>val.type</p>
                <p>val.text</p>
            </div>
        </div>

js数据绑定html

contents:[{
            name:"test1",
            values:[{
                text:"test11",
                type:"string"
            },{
                text:11,
                type:"number"
            }]
        },{
        name:"test2",
        values:[{
            text:"test21",
            type:"string"
        },{
            text:121,
            type:"number"
        }]
}] 

 改变数据方法 vue

changeVal:function(index){
            this.contents[index].name="change"
        }

理论上说,当这个方法触发时,视图内容应该也会对应的发生改变。其实是方法执行时视图没有响应,但数据发生了改变。java

vue是经过检测get,set才得知数据是否更新的,而对于数组来讲,是没有get,set方法的,因此须要咱们本身手动触发,须要发送消息通知vueapi

下面是改后的方法数组

 changeVal:function(index){
            this.contents[index].name="change";
        Vue.set(this.contents, index, this.contents[index]);
        }

set方法具体点击  https://cn.vuejs.org/v2/api/#Vue-setthis

相关文章
相关标签/搜索