Skip to content

Commit 28d5fd7

Browse files
authored
fix(runtime-core): vnode.el is null in watcher after rerendering (#2295)
fix #2170
1 parent db6c2df commit 28d5fd7

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

packages/runtime-core/__tests__/rendererComponent.spec.ts

+79
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,83 @@ describe('renderer: component', () => {
137137
await nextTick()
138138
expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
139139
})
140+
141+
// #2170
142+
test('should have access to instance’s “$el” property in watcher when setting instance data', async () => {
143+
function returnThis(this: any) {
144+
return this
145+
}
146+
const dataWatchSpy = jest.fn(returnThis)
147+
let instance: any
148+
const Comp = {
149+
data() {
150+
return {
151+
testData: undefined
152+
}
153+
},
154+
155+
watch: {
156+
testData() {
157+
// @ts-ignore
158+
dataWatchSpy(this.$el)
159+
}
160+
},
161+
162+
created() {
163+
instance = this
164+
},
165+
166+
render() {
167+
return h('div')
168+
}
169+
}
170+
171+
const root = nodeOps.createElement('div')
172+
render(h(Comp), root)
173+
174+
expect(dataWatchSpy).not.toHaveBeenCalled()
175+
instance.testData = 'data'
176+
177+
await nextTick()
178+
expect(dataWatchSpy).toHaveBeenCalledWith(instance.$el)
179+
})
180+
181+
// #2170
182+
test('should have access to instance’s “$el” property in watcher when rendereing with watched prop', async () => {
183+
function returnThis(this: any) {
184+
return this
185+
}
186+
const propWatchSpy = jest.fn(returnThis)
187+
let instance: any
188+
const Comp = {
189+
props: {
190+
testProp: String
191+
},
192+
193+
watch: {
194+
testProp() {
195+
// @ts-ignore
196+
propWatchSpy(this.$el)
197+
}
198+
},
199+
200+
created() {
201+
instance = this
202+
},
203+
204+
render() {
205+
return h('div')
206+
}
207+
}
208+
209+
const root = nodeOps.createElement('div')
210+
211+
render(h(Comp), root)
212+
await nextTick()
213+
expect(propWatchSpy).not.toHaveBeenCalled()
214+
215+
render(h(Comp, { testProp: 'prop ' }), root)
216+
await nextTick()
217+
expect(propWatchSpy).toHaveBeenCalledWith(instance.$el)
218+
})
140219
})

packages/runtime-core/src/renderer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1425,11 +1425,11 @@ function baseCreateRenderer(
14251425
}
14261426

14271427
if (next) {
1428+
next.el = vnode.el
14281429
updateComponentPreRender(instance, next, optimized)
14291430
} else {
14301431
next = vnode
14311432
}
1432-
next.el = vnode.el
14331433

14341434
// beforeUpdate hook
14351435
if (bu) {

0 commit comments

Comments
 (0)