You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
2 years ago
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="UTF-8">
|
||
|
<title>Vue.set的使用</title>
|
||
|
<script type="text/javascript" src="../vue.js"></script>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div id="root">
|
||
|
<h2>学校名称:{{name}}</h2>
|
||
|
<h2>学校地址:{{address}}</h2>
|
||
|
<!-- <h2>校长:{{leader}}</h2>-->
|
||
|
<hr/>
|
||
|
<button @click="addSex">添加性别</button>
|
||
|
<h2>学生姓名:{{student.name}}</h2>
|
||
|
<h2>学生年龄:真实{{student.age.rAge}},对外{{student.age.sAge}}</h2>
|
||
|
<h2 v-if="student.sex">学生性别:{{student.sex}}</h2>
|
||
|
<!-- Vue.set(vm._data.student, 'sex', '男')-->
|
||
|
<!-- Vue.set(vm.student, 'sex', '男')-->
|
||
|
<!-- vm.$set(vm._data.student, 'sex', '男')-->
|
||
|
<h2>朋友</h2>
|
||
|
<ul>
|
||
|
<li v-for="(f,index) in student.friends" :key="index">
|
||
|
<h2>{{f.name}} - {{f.age}}</h2>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</body>
|
||
|
<script type="text/javascript">
|
||
|
Vue.config.productionTip = false
|
||
|
const vm = new Vue({
|
||
|
el: '#root',
|
||
|
data: {
|
||
|
name: '哈哈',
|
||
|
address: '北京',
|
||
|
student: {
|
||
|
name: 'tom',
|
||
|
age: {
|
||
|
rAge: 40,
|
||
|
sAge: 29,
|
||
|
},
|
||
|
friends:[
|
||
|
{name: 'jerry', age: 35},
|
||
|
{name: 'tony', age: 28}
|
||
|
]
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
addSex() {
|
||
|
Vue.set(this.student, "sex", "男")
|
||
|
// vm.$set(this.student, 'sex', '男')
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
</script>
|
||
|
</html>
|