forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
76 lines (67 loc) · 2.08 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
import Vue from 'vue'
import VueRouter from 'vue-router'
// track number of popstate listeners
let numPopstateListeners = 0
const listenerCountDiv = document.getElementById('popcount')
listenerCountDiv.textContent = 0
const originalAddEventListener = window.addEventListener
const originalRemoveEventListener = window.removeEventListener
window.addEventListener = function (name, handler) {
if (name === 'popstate') {
listenerCountDiv.textContent =
++numPopstateListeners
}
return originalAddEventListener.apply(this, arguments)
}
window.removeEventListener = function (name, handler) {
if (name === 'popstate') {
listenerCountDiv.textContent =
--numPopstateListeners
}
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)
const looper = [1, 2, 3]
looper.forEach((n) => {
const mountEl = document.getElementById('mount' + n)
mountEl.addEventListener('click', () => {
// 2. Define route components
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
// 3. Create the router
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/', component: Home },
{ path: '/foo', component: Foo }
]
})
// 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-${n}">
<h1>Basic</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/foo">/foo</router-link></li>
</ul>
<router-view class="view"></router-view>
<button id="unmount${n}" v-on:click="teardown">Teardown app</button>
</div>
`,
methods: {
teardown () {
this.$destroy()
this.$el.innerHTML = ''
}
}
}).$mount('#app-' + n)
})
})