Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromvuejs/vue

Commit318f29f

Browse files
javoskiyyx990803
authored andcommitted
fix(core): disable dependency collection in lifecycle hooks and data getter (vuejs#7596)
This fixes the parent being updated more than necessary due to collecting child propsas dependencies during its own update computation.fixvuejs#7573
1 parentd7d9b00 commit318f29f

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

‎src/core/instance/lifecycle.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createEmptyVNode } from '../vdom/vnode'
77
import{observerState}from'../observer/index'
88
import{updateComponentListeners}from'./events'
99
import{resolveSlots}from'./render-helpers/resolve-slots'
10+
import{pushTarget,popTarget}from'../observer/dep'
1011

1112
import{
1213
warn,
@@ -315,6 +316,8 @@ export function deactivateChildComponent (vm: Component, direct?: boolean) {
315316
}
316317

317318
exportfunctioncallHook(vm:Component,hook:string){
319+
// #7573 disable dep collection when invoking lifecycle hooks
320+
pushTarget()
318321
consthandlers=vm.$options[hook]
319322
if(handlers){
320323
for(leti=0,j=handlers.length;i<j;i++){
@@ -328,4 +331,5 @@ export function callHook (vm: Component, hook: string) {
328331
if(vm._hasHookEvent){
329332
vm.$emit('hook:'+hook)
330333
}
334+
popTarget()
331335
}

‎src/core/instance/state.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*@flow */
22

33
importconfigfrom'../config'
4-
importDepfrom'../observer/dep'
54
importWatcherfrom'../observer/watcher'
5+
importDep,{pushTarget,popTarget}from'../observer/dep'
66
import{isUpdatingChildComponent}from'./lifecycle'
77

88
import{
@@ -150,11 +150,15 @@ function initData (vm: Component) {
150150
}
151151

152152
exportfunctiongetData(data:Function,vm:Component):any{
153+
// #7573 disable dep collection when invoking data getters
154+
pushTarget()
153155
try{
154156
returndata.call(vm,vm)
155157
}catch(e){
156158
handleError(e,vm,`data()`)
157159
return{}
160+
}finally{
161+
popTarget()
158162
}
159163
}
160164

‎src/core/observer/dep.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class Dep {
4848
Dep.target=null
4949
consttargetStack=[]
5050

51-
exportfunctionpushTarget(_target:Watcher){
51+
exportfunctionpushTarget(_target:?Watcher){
5252
if(Dep.target)targetStack.push(Dep.target)
5353
Dep.target=_target
5454
}

‎test/unit/features/options/data.spec.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ describe('Options data', () => {
9393
expect(vm.$refs.test.b).toBe(1)
9494
})
9595

96+
it('props should not be reactive',done=>{
97+
letcalls=0
98+
constvm=newVue({
99+
template:`<child :msg="msg"></child>`,
100+
data:{
101+
msg:'hello'
102+
},
103+
beforeUpdate(){calls++},
104+
components:{
105+
child:{
106+
template:`<span>{{ localMsg }}</span>`,
107+
props:['msg'],
108+
data(){
109+
return{localMsg:this.msg}
110+
},
111+
computed:{
112+
computedMsg(){
113+
returnthis.msg+' world'
114+
}
115+
}
116+
}
117+
}
118+
}).$mount()
119+
constchild=vm.$children[0]
120+
vm.msg='hi'
121+
waitForUpdate(()=>{
122+
expect(child.localMsg).toBe('hello')
123+
expect(child.computedMsg).toBe('hi world')
124+
expect(calls).toBe(1)
125+
}).then(done)
126+
})
127+
96128
it('should have access to methods',()=>{
97129
constvm=newVue({
98130
methods:{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp