forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (67 loc) · 1.75 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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')