Skip to content

Commit 2a3f030

Browse files
committed
chore: 增加Object.assign实现对象新增属性响应式示例
1 parent e35bdc3 commit 2a3f030

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Diff for: .vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
// 设置不检查 JavaScript 的语法问题,防止 flow 报错
33
"javascript.validate.enable": false,
4-
"commentTranslate.source": "Bing"
4+
"commentTranslate.source": "Bing",
5+
"liveServer.settings.port": 5501
56
}

Diff for: examples/00-vue-analysis/17-reactive-warning.html

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
又会使用 src\shared\util.js 中的 toString 方法将该 msg 对象转成字符串,
1313
此时又会进行一次依赖收集
1414
-->
15-
<!-- <h3>{{ msg }}</h3> -->
16-
<ul>
15+
<h3>{{ msg }}</h3>
16+
<!-- <ul>
1717
<li v-for="item in items">{{ item }}</li>
18-
</ul>
18+
</ul> -->
1919
<button @click="add">add</button>
2020
<button @click="change">change</button>
2121
</div>
@@ -46,10 +46,10 @@
4646
el: '#app',
4747
data () {
4848
return {
49-
// msg: {
50-
// a: 'hello'
51-
// },
52-
items: [1, 2]
49+
msg: {
50+
a: 'hello'
51+
},
52+
// items: [1, 2]
5353
}
5454
},
5555
methods: {
@@ -66,7 +66,10 @@
6666
// this.msg.b = 'Vue'
6767

6868
// 正确写法
69-
Vue.set(this.msg, 'b', 'Vue')
69+
// Vue.set(this.msg, 'b', 'Vue')
70+
// Object.assign 能够生效是由于 defineReactive setter 中的 “childOb = !shallow && observe(newVal)” 这样代码,
71+
// 即会对传入的 对象 重新做依赖收集!
72+
this.msg = Object.assign({}, this.msg, { b: 'Vue' })
7073

7174
// 错误写法
7275
// this.items[2] = 4

0 commit comments

Comments
 (0)