Skip to content

Commit b60cd83

Browse files
committed
keep-alive re-activated component should get updated props (fix vuejs#4237)
1 parent cbc1fbc commit b60cd83

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/core/vdom/create-component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ function init (vnode: VNodeWithData, hydrating: boolean) {
154154
if (!vnode.child || vnode.child._isDestroyed) {
155155
const child = vnode.child = createComponentInstanceForVnode(vnode, activeInstance)
156156
child.$mount(hydrating ? vnode.elm : undefined, hydrating)
157+
} else if (vnode.data.keepAlive) {
158+
// kept-alive components, treat as a patch
159+
prepatch(vnode, vnode)
157160
}
158161
}
159162

test/unit/features/component/component-keep-alive.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ describe('Component keep-alive', () => {
107107
}).then(done)
108108
})
109109

110+
// #4237
111+
it('should update latest props/listners for a re-activated component', done => {
112+
const one = {
113+
props: ['prop'],
114+
template: `<div>one {{ prop }}</div>`
115+
}
116+
const two = {
117+
props: ['prop'],
118+
template: `<div>two {{ prop }}</div>`
119+
}
120+
const vm = new Vue({
121+
data: { view: 'one', n: 1 },
122+
template: `
123+
<div>
124+
<keep-alive>
125+
<component :is="view" :prop="n"></component>
126+
</keep-alive>
127+
</div>
128+
`,
129+
components: { one, two }
130+
}).$mount()
131+
132+
expect(vm.$el.textContent).toBe('one 1')
133+
vm.n++
134+
waitForUpdate(() => {
135+
expect(vm.$el.textContent).toBe('one 2')
136+
vm.view = 'two'
137+
}).then(() => {
138+
expect(vm.$el.textContent).toBe('two 2')
139+
}).then(done)
140+
})
141+
110142
if (!isIE9) {
111143
it('with transition-mode out-in', done => {
112144
let next

0 commit comments

Comments
 (0)