forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
82 lines (76 loc) · 2.7 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
75
76
77
78
79
80
81
82
import Vue from 'vue'
import VueRouter from 'vue-router'
// track number of popstate listeners
let numPopstateListeners = 0
const listenerCountDiv = document.createElement('div')
listenerCountDiv.id = 'popstate-count'
listenerCountDiv.textContent = numPopstateListeners + ' popstate listeners'
document.body.appendChild(listenerCountDiv)
const originalAddEventListener = window.addEventListener
const originalRemoveEventListener = window.removeEventListener
window.addEventListener = function (name, handler) {
if (name === 'popstate') {
listenerCountDiv.textContent =
++numPopstateListeners + ' popstate listeners'
}
return originalAddEventListener.apply(this, arguments)
}
window.removeEventListener = function (name, handler) {
if (name === 'popstate') {
listenerCountDiv.textContent =
--numPopstateListeners + ' popstate listeners'
}
return originalRemoveEventListener.apply(this, arguments)
}
// 1. Use plugin.
// This installs <router-view> and <router-link>,
// and injects $router and $route to all router-enabled child components
Vue.use(VueRouter)
// 2. Define route components
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Unicode = { template: '<div>unicode: {{ $route.params.unicode }}</div>' }
// 3. Create the router
const router = new VueRouter({
mode: 'hash',
base: __dirname,
routes: [
{ path: '/', component: Home }, // all paths are defined without the hash.
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar },
{ path: '/é', component: Unicode },
{ path: '/é/:unicode', component: Unicode }
]
})
// 4. Create and mount root instance.
// Make sure to inject the router.
// Route components will be rendered inside <router-view>.
new Vue({
router,
template: `
<div id="app">
<h1>Mode: 'hash'</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/foo">/foo</router-link></li>
<li><router-link to="/bar">/bar</router-link></li>
<router-link tag="li" to="/bar">/bar</router-link>
<li><router-link to="/é">/é</router-link></li>
<li><router-link to="/é/ñ">/é/ñ</router-link></li>
<li><router-link to="/é/ñ?t=%25ñ">/é/ñ?t=%ñ</router-link></li>
<li><router-link to="/é/ñ#é">/é/ñ#é</router-link></li>
</ul>
<pre id="query-t">{{ $route.query.t }}</pre>
<pre id="hash">{{ $route.hash }}</pre>
<router-view class="view"></router-view>
<button id="teardown-app" v-on:click="teardown">Teardown app</button>
</div>
`,
methods: {
teardown () {
this.$destroy()
this.$el.innerHTML = ''
}
}
}).$mount('#app')