-
Notifications
You must be signed in to change notification settings - Fork 5
/
20.vueSet使用.html
69 lines (65 loc) · 1.62 KB
/
20.vueSet使用.html
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
67
68
69
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
{{message}}
<div class="obj" v-for="(item, index) in obj" :key="index">
<span>{{item.name}}</span><span>{{item.age}}</span>{{item.type}}
</div>
<button @click="getData">改变</button>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
obj: []
},
mounted() {
this.obj = [
{
id: 1,
name: '小米',
sex: '女',
age: 18
}, {
id: 2,
name: '华为',
sex: '男',
age: 10
}, {
id: 3,
name: '腾讯',
sex: '女',
age: 28
},
]
},
methods: {
getData() {
console.log('改变');
this.obj.forEach((item, index) => {
if (item.age > 20) {
// 虽然改变数据,但是无法改变视图
// item.type = '大龄剩女'
this.$set(this.obj[index], 'type', '大龄剩女')
} else {
// item.type = '再等等'
this.$set(this.obj[index], 'type', '再等等')
}
console.log(this.obj[index]);
// this.$set(this.obj[index], type, 'item.type')
});
console.log(this.obj);
}
}
})
</script>
</body>
</html>