1、添加对象某个属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue.set()的使用</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="root">
<h2>学校地址:{{address}}</h2>
<h2>学校名称:{{name}}</h2>
<hr/>
<h1 >学生信息</h1>
<button @click="addsex">点击添加学生性别</button>
<h2>学生名字:{{student.name}}</h2>
<h2 v-if="student.sex">性别:{{student.sex}}</h2>
<h2>学生真实年龄:{{student.age.rage}}</h2>
<h2>学生对外年龄:{{student.age.sage}}</h2>
<h2>学生朋友们</h2>
<h2>
<ul>
<li v-for="(p,index) in student.friends" :key="index"> {{p.name}}--{{p.age}}</li>
</ul>
</h2>
</div>
<script type="text/javascript">
Vue.config.productionTip=false;
const vm = new Vue({
el:"#root",
data:{
address:'尚硅谷',
name:'北京',
student:{
name: 'tom',
age:{
rage:40,
sage:29
},
friends:[
{name:'jeery',age:35},
{name:'tony',age:36}
]
}
},
methods:{
addsex(){
Vue.set(this.student,'sex','男')
}
}
})
</script>
</body>
</html>
2、另外一种写法
this.$set(this.student,'sex','男');