Skip to content

Commit e6d8fd2

Browse files
zrh122posva
authored andcommitted
fix(router-view): add condition to see whether the tree is inactive (fix #2552) (#2592)
Fix #2552 <!-- Please make sure to read the Pull Request Guidelines: https://github.com/vuejs/vue/blob/dev/.github/CONTRIBUTING.md#pull-request-guidelines -->
1 parent 6974a6f commit e6d8fd2

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed

examples/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ <h1>Vue Router Examples</h1>
2525
<li><a href="auth-flow">Auth Flow</a></li>
2626
<li><a href="discrete-components">Discrete Components</a></li>
2727
<li><a href="nested-router">Nested Routers</a></li>
28+
<li><a href="keepalive-view">Keepalive View</a></li>
2829
</ul>
2930
</body>
3031
</html>

examples/keepalive-view/app.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Wrap = { template: '<div>child: <router-view></router-view></div>' }
7+
8+
const Index = {
9+
template: '<wrap />',
10+
components: {
11+
Wrap
12+
}
13+
}
14+
15+
const IndexChild1 = { template: '<div class="current">index child1</div>' }
16+
const IndexChild2 = { template: '<div class="current">index child2</div>' }
17+
18+
const Home = { template: '<div class="current">home</div>' }
19+
20+
const router = new VueRouter({
21+
mode: 'history',
22+
base: __dirname,
23+
routes: [
24+
{
25+
path: '/index',
26+
component: Index,
27+
children: [
28+
{
29+
path: 'child1',
30+
component: IndexChild1
31+
},
32+
{
33+
path: 'child2',
34+
component: IndexChild2
35+
}
36+
]
37+
},
38+
{
39+
path: '/home',
40+
component: Home
41+
}
42+
]
43+
})
44+
45+
new Vue({
46+
router,
47+
template: `
48+
<div id="app">
49+
<ul>
50+
<li><router-link tag="a" to="/index/child1">index child 1</router-link></li>
51+
<li><router-link tag="a" to="/index/child2">index child 2</router-link></li>
52+
<li><router-link tag="a" to="/home">home</router-link></li>
53+
</ul>
54+
<keep-alive>
55+
<router-view></router-view>
56+
</keep-alive>
57+
</div>
58+
`
59+
}).$mount('#app')

examples/keepalive-view/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<style>
4+
a.router-link-active, li.router-link-active a {
5+
color: #f66;
6+
}
7+
a.router-link-exact-active, li.router-link-exact-active a {
8+
border-bottom: 1px solid #f66;
9+
}
10+
</style>
11+
<a href="/">&larr; Examples index</a>
12+
<div id="app"></div>
13+
<script src="/__build__/shared.chunk.js"></script>
14+
<script src="/__build__/keepalive-view.js"></script>

src/components/view.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export default {
2626
let depth = 0
2727
let inactive = false
2828
while (parent && parent._routerRoot !== parent) {
29-
if (parent.$vnode && parent.$vnode.data.routerView) {
30-
depth++
31-
}
32-
if (parent._inactive) {
33-
inactive = true
29+
const vnodeData = parent.$vnode && parent.$vnode.data
30+
if (vnodeData) {
31+
if (vnodeData.routerView) {
32+
depth++
33+
}
34+
if (vnodeData.keepAlive && parent._inactive) {
35+
inactive = true
36+
}
3437
}
3538
parent = parent.$parent
3639
}

test/e2e/specs/keepalive-view.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
'keepalive view': function (browser) {
3+
browser
4+
.url('http://localhost:8080/keepalive-view/')
5+
.waitForElementVisible('#app', 1000)
6+
.assert.count('li a', 3)
7+
8+
.click('li:nth-child(1) a')
9+
.assert.containsText('.current', 'index child1')
10+
11+
.click('li:nth-child(2) a')
12+
.assert.containsText('.current', 'index child2')
13+
14+
.click('li:nth-child(3) a')
15+
.assert.containsText('.current', 'home')
16+
17+
// back to index child1 and check it
18+
.click('li:nth-child(1) a')
19+
.assert.containsText('.current', 'index child1')
20+
.end()
21+
}
22+
}

0 commit comments

Comments
 (0)