Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router-view): register instance in init hook (fix #2561) #2689

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h1>Vue Router Examples</h1>
<li><a href="auth-flow">Auth Flow</a></li>
<li><a href="discrete-components">Discrete Components</a></li>
<li><a href="nested-router">Nested Routers</a></li>
<li><a href="navigation-guard-callback">Navigation Guard Callback</a></li>
</ul>
</body>
</html>
74 changes: 74 additions & 0 deletions examples/navigation-guard-callback/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Foo = {
template: '<div class="current">foo</div>',
beforeRouteEnter (to, from, next) {
// in-component beforeRouteEnter hook
next(vm => {
// print log
app.logText = 'beforeRouteEnter callback emited'
})
}
}

const Bar = { template: '<div class="current">bar</div>' }

const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
// foo1
{ path: '/foo1', component: Foo },

// foo2, same component as foo1
{ path: '/foo2', component: Foo },

// bar
{ path: '/bar', component: Bar }
]
})

const app = new Vue({
data: {
applyKeepAlive: false,
logText: ''
},
router,
template: `
<div id="app">
<h1>Navigation Guard Callback</h1>
<ul>
<li><router-link to="/" id="root">/</router-link></li>
<li><router-link to="/foo1" id="foo1">/foo1</router-link></li>
<li><router-link to="/foo2" id="foo2">/foo2</router-link></li>
<li><router-link to="/bar" id="bar">/bar</router-link></li>
</ul>
<div v-if="applyKeepAlive" class="keep-alive-wrap">
<keep-alive>
<router-view class="view"></router-view>
</keep-alive>
</div>
<div v-else>
<router-view class="view"></router-view>
</div>
<div class="log">{{logText}}</div>
<div>
<button class="btn-apply-keep-alive" @click="toggle">apply keep-alive</button>
<button class="btn-clear-log" @click="clear">clear log</button>
</div>
</div>
`,
methods: {
toggle () {
// wrap router-view with keep-alive
this.applyKeepAlive = true
},
clear () {
// clear log
this.logText = ''
}
}
}).$mount('#app')
10 changes: 10 additions & 0 deletions examples/navigation-guard-callback/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<link rel="stylesheet" href="/global.css">
<style>
.log { border: 1px solid #333; margin: 20px 0; min-height: 100px; }
a.router-link-exact-active{ color: #f66; }
</style>
<a href="/">&larr; Examples index</a>
<div id="app"></div>
<script src="/__build__/shared.chunk.js"></script>
<script src="/__build__/navigation-guard-callback.js"></script>
11 changes: 11 additions & 0 deletions src/components/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export default {
matched.instances[name] = vnode.componentInstance
}

// register instance in init hook
// in case kept-alive component be actived when routes changed
data.hook.init = (vnode) => {
if (vnode.data.keepAlive &&
vnode.componentInstance &&
vnode.componentInstance !== matched.instances[name]
) {
matched.instances[name] = vnode.componentInstance
}
}

// resolve props
let propsToPass = data.props = resolveProps(route, matched.props && matched.props[name])
if (propsToPass) {
Expand Down
48 changes: 48 additions & 0 deletions test/e2e/specs/navigation-guard-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = {
'navigation guard callback': function (browser) {
browser
.url('http://localhost:8080/navigation-guard-callback/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 4)

.click('.btn-clear-log')
.assert.containsText('.log', '')
.click('#foo1')
.assert.containsText('.current', 'foo')
.assert.containsText('.log', 'beforeRouteEnter callback emited')

.click('#bar')
.assert.containsText('.current', 'bar')

.click('.btn-clear-log')
.assert.containsText('.log', '')
.click('#foo2')
.assert.containsText('.current', 'foo')
.assert.containsText('.log', 'beforeRouteEnter callback emited')

// back to root path
.click('#root')
.assert.urlEquals('http://localhost:8080/navigation-guard-callback/')

// apply keep-alive
.click('.btn-apply-keep-alive')
.assert.elementPresent('.keep-alive-wrap')

// do the same
.click('.btn-clear-log')
.assert.containsText('.log', '')
.click('#foo1')
.assert.containsText('.current', 'foo')
.assert.containsText('.log', 'beforeRouteEnter callback emited')

.click('#bar')
.assert.containsText('.current', 'bar')

.click('.btn-clear-log')
.assert.containsText('.log', '')
.click('#foo2')
.assert.containsText('.current', 'foo')
.assert.containsText('.log', 'beforeRouteEnter callback emited')
.end()
}
}