来自:https://cn.vuejs.org/v2/guide/reactivity.html

1
2
// Vue.set(this.user,this.property,"1_1"); 这种写法也是对的
this.$set(this.user,this.property,"1_1");

Vue 不允许动态添加根级响应式属性,所以你必须在初始化实例前声明根级响应式属性,哪怕只是一个空值
受现代 JavaScript 的限制 (以及废弃 Object.observe),Vue 不能检测到对象属性的添加或删除。由于 Vue 会在初始化实例时对属性执行 getter/setter 转化过程,所以属性必须在 data 对象上存在才能让 Vue 转换它,这样才能让它是响应的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/webpage/common/taglibs.jspf"%>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<meta name="decorator" content="single"/>
<script src="/static/common/js/vue.js"></script>


</head>
<body>
<div id="app">
<div v-for="n in arr">
n-{{n}}
</div>
<template v-for="(v,k) in user">
<div>
<span>{{k}}=</span>
<span>{{v}}</span>
</div>
</template>
<input v-model="property">
<button @click="addProperty">addProperty</button>
<button @click="rootAdd">rootAdd</button>
<button @click="arrlist">arrlist</button>
</div>





<script>

var app=new Vue({
el:"#app",
data:{
property:"xp",
user:{
name:"user1",
},
arr:[1,2,3]
},
methods:{
arrlist:function(){
Vue.set(this.arr,0,123);
},
addProperty:function(){
// this.user.age=123;
// Vue.set(this.user,this.property,"1_1"); 这种写法也是对的
this.$set(this.user,this.property,"1_1");
console.info(this.user)
},
rootAdd:function(){
//Vue 不允许动态添加根级响应式属性,所以你必须在初始化实例前声明根级响应式属性,哪怕只是一个空值
this.$set(this.data,"a",1);
},

}
})
</script>
</body>

</html>