Skip to content

Commit

Permalink
fix(router-view): fix deeply nested keep-alive router-views displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
zrh122 committed Sep 17, 2019
1 parent 41b9ac8 commit 6114e6f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
24 changes: 24 additions & 0 deletions examples/keepalive-view/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const IndexChild2 = { template: '<div>index child2</div>' }

const Home = { template: '<div>home</div>' }

const ViewWithKeepalive = { template: '<keep-alive><router-view></router-view></keep-alive>' }

const router = new VueRouter({
mode: 'history',
base: __dirname,
Expand Down Expand Up @@ -58,6 +60,26 @@ const router = new VueRouter({
path: '/with-guard2',
name: 'with-guard2',
component: WithGuard
},
{
path: '/one',
component: ViewWithKeepalive,
children: [
{
path: 'two',
component: ViewWithKeepalive,
children: [
{
path: 'child1',
component: IndexChild1
},
{
path: 'child2',
component: IndexChild2
}
]
}
]
}
]
})
Expand All @@ -72,6 +94,8 @@ new Vue({
<li><router-link to="/home">/home</router-link></li>
<li><router-link to="/with-guard1">/with-guard1</router-link></li>
<li><router-link to="/with-guard2">/with-guard2</router-link></li>
<li><router-link to="/one/two/child1">/one/two/child1</router-link></li>
<li><router-link to="/one/two/child2">/one/two/child2</router-link></li>
</ul>
<keep-alive>
<router-view class="view"></router-view>
Expand Down
13 changes: 5 additions & 8 deletions src/components/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ export default {
let depth = 0
let inactive = false
while (parent && parent._routerRoot !== parent) {
const vnodeData = parent.$vnode && parent.$vnode.data
if (vnodeData) {
if (vnodeData.routerView) {
depth++
}
if (vnodeData.keepAlive && parent._inactive) {
inactive = true
}
if (parent.$vnode && parent.$vnode.data.routerView) {
depth++
}
if (parent._directInactive && parent._inactive) {
inactive = true
}
parent = parent.$parent
}
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/specs/keepalive-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
browser
.url('http://localhost:8080/keepalive-view/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 5)
.assert.count('li a', 7)

.click('li:nth-child(1) a')
.assert.containsText('.view', 'index child1')
Expand All @@ -35,6 +35,15 @@ module.exports = {
.click('li:nth-child(4) a')
.assert.containsText('.view', 'with-guard1: 3')

// keep-alive deeply nested router-views
// https://github.com/vuejs/vue-router/issues/2923
.click('li:nth-child(6) a')
.assert.containsText('.view', 'index child1')
.click('li:nth-child(3) a')
.assert.containsText('.view', 'home')
.click('li:nth-child(7) a')
.assert.containsText('.view', 'index child2')

.end()
}
}

0 comments on commit 6114e6f

Please sign in to comment.