Skip to content

Commit c3abdf6

Browse files
committed
fix(router-view): register instance in init hook
Fix #2561 Close ##2689 Squashed commit of the following: commit e18dd2f660b2154b26e6afa9d60ac76b047b8e40 Author: Eduardo San Martin Morote <[email protected]> Date: Mon Apr 15 12:54:55 2019 +0200 refactor: use keepalive example instead commit 2c458af27f5371c690c2832d59e0b670e43a17fa Author: Eduardo San Martin Morote <[email protected]> Date: Mon Apr 15 11:48:24 2019 +0200 refactor: remove unused classes commit 8467722640ee11baecabf52dd91bbc7d90d370a0 Author: zrh122 <[email protected]> Date: Sun Mar 31 05:26:05 2019 +0800 test: add e2e test for #2561 commit af0ff980b4ca0b32637aa34d692587a790790947 Author: zrh122 <[email protected]> Date: Sat Mar 30 13:57:38 2019 +0800 fix(router-view): register instance in init hook close #2561
1 parent aff474e commit c3abdf6

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

examples/keepalive-view/app.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ const Index = {
1212
}
1313
}
1414

15-
const IndexChild1 = { template: '<div class="current">index child1</div>' }
16-
const IndexChild2 = { template: '<div class="current">index child2</div>' }
15+
const WithGuard = {
16+
template: '<div>{{ $route.name }}: {{ n }}</div>',
17+
data: () => ({ n: 0 }),
18+
beforeRouteEnter (to, from, next) {
19+
next(vm => {
20+
vm.n++
21+
})
22+
}
23+
}
1724

18-
const Home = { template: '<div class="current">home</div>' }
25+
const IndexChild1 = { template: '<div>index child1</div>' }
26+
const IndexChild2 = { template: '<div>index child2</div>' }
27+
28+
const Home = { template: '<div>home</div>' }
1929

2030
const router = new VueRouter({
2131
mode: 'history',
@@ -38,6 +48,16 @@ const router = new VueRouter({
3848
{
3949
path: '/home',
4050
component: Home
51+
},
52+
{
53+
path: '/with-guard1',
54+
name: 'with-guard1',
55+
component: WithGuard
56+
},
57+
{
58+
path: '/with-guard2',
59+
name: 'with-guard2',
60+
component: WithGuard
4161
}
4262
]
4363
})
@@ -47,12 +67,14 @@ new Vue({
4767
template: `
4868
<div id="app">
4969
<ul>
50-
<li><router-link to="/index/child1">index child 1</router-link></li>
51-
<li><router-link to="/index/child2">index child 2</router-link></li>
52-
<li><router-link to="/home">home</router-link></li>
70+
<li><router-link to="/index/child1">/index/child1</router-link></li>
71+
<li><router-link to="/index/child2">/index/child2</router-link></li>
72+
<li><router-link to="/home">/home</router-link></li>
73+
<li><router-link to="/with-guard1">/with-guard1</router-link></li>
74+
<li><router-link to="/with-guard2">/with-guard2</router-link></li>
5375
</ul>
5476
<keep-alive>
55-
<router-view></router-view>
77+
<router-view class="view"></router-view>
5678
</keep-alive>
5779
</div>
5880
`

src/components/view.js

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ export default {
7272
matched.instances[name] = vnode.componentInstance
7373
}
7474

75+
// register instance in init hook
76+
// in case kept-alive component be actived when routes changed
77+
data.hook.init = (vnode) => {
78+
if (vnode.data.keepAlive &&
79+
vnode.componentInstance &&
80+
vnode.componentInstance !== matched.instances[name]
81+
) {
82+
matched.instances[name] = vnode.componentInstance
83+
}
84+
}
85+
7586
// resolve props
7687
let propsToPass = data.props = resolveProps(route, matched.props && matched.props[name])
7788
if (propsToPass) {

test/e2e/specs/keepalive-view.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,30 @@ module.exports = {
33
browser
44
.url('http://localhost:8080/keepalive-view/')
55
.waitForElementVisible('#app', 1000)
6-
.assert.count('li a', 3)
6+
.assert.count('li a', 5)
77

88
.click('li:nth-child(1) a')
9-
.assert.containsText('.current', 'index child1')
9+
.assert.containsText('.view', 'index child1')
1010

1111
.click('li:nth-child(2) a')
12-
.assert.containsText('.current', 'index child2')
12+
.assert.containsText('.view', 'index child2')
1313

1414
.click('li:nth-child(3) a')
15-
.assert.containsText('.current', 'home')
15+
.assert.containsText('.view', 'home')
1616

1717
// back to index child1 and check it
1818
.click('li:nth-child(1) a')
19-
.assert.containsText('.current', 'index child1')
19+
.assert.containsText('.view', 'index child1')
20+
21+
// beforeRouteEnter guard with keep alive
22+
// https://github.com/vuejs/vue-router/issues/2561
23+
.click('li:nth-child(4) a')
24+
.assert.containsText('.view', 'with-guard1: 1')
25+
.click('li:nth-child(5) a')
26+
.assert.containsText('.view', 'with-guard2: 2')
27+
.click('li:nth-child(4) a')
28+
.assert.containsText('.view', 'with-guard1: 3')
29+
2030
.end()
2131
}
2232
}

0 commit comments

Comments
 (0)