/
index.ts
131 lines (109 loc) · 3.23 KB
/
index.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { createRouter, createWebHistory } from '../../src'
import { RouteComponent } from '../../src/types'
import { createApp, ref, watchEffect, App, inject } from 'vue'
const Home: RouteComponent = {
template: `<div class="home">Home</div>`,
}
const User: RouteComponent = {
template: `<div class="user">User {{ $route.params.id }}. Updated <span class="count">{{ count }}</span></div>`,
data: () => ({ count: 0 }),
beforeRouteEnter(to, from, next) {
next(vm => {
// @ts-expect-error
console.log('enter from ', vm.id)
})
},
beforeRouteUpdate(to, from, next) {
// this.count++
next()
},
setup() {
const id = inject('id')!
return { id }
},
}
let looper = [1, 2, 3]
const NamedViews: RouteComponent[] = looper.map(i => ({
name: 'part-' + i,
template: `<div class="named" id="part-${i}">Part ${i}. Updated <span class="count">{{ count }}</span></div>`,
data: () => ({ count: 0 }),
beforeRouteUpdate(to, from, next) {
console.log('update of', i)
// @ts-expect-error
this.count++
next()
},
}))
// path popstate listeners to track the call count
let activePopStateListeners = ref(0)
let guardCallCount = ref(0)
const popCountDiv = document.getElementById('popcount')!
const guardCountDiv = document.getElementById('guardcount')!
watchEffect(() => {
popCountDiv.innerHTML = '' + activePopStateListeners.value
})
watchEffect(() => {
guardCountDiv.innerHTML = '' + guardCallCount.value
})
const originalAddEventListener = window.addEventListener
const originalRemoveEventListener = window.removeEventListener
window.addEventListener = function (name: string, handler: any) {
if (name === 'popstate') {
activePopStateListeners.value++
}
return originalAddEventListener.call(this, name, handler)
}
window.removeEventListener = function (name: string, handler: any) {
if (name === 'popstate') {
activePopStateListeners.value--
}
return originalRemoveEventListener.call(this, name, handler)
}
const router = createRouter({
history: createWebHistory('/' + __dirname),
routes: [
{ path: '/', component: Home },
{
path: '/users/:id',
components: {
default: User,
...NamedViews.reduce(
(routeComponents, component) => ({
...routeComponents,
[component.name!]: component,
}),
{} as Record<string, RouteComponent>
),
},
},
],
})
router.beforeEach((to, from, next) => {
guardCallCount.value++
next()
})
let apps: Array<App | null> = [null, null, null]
looper.forEach((n, i) => {
let mountBtn = document.getElementById('mount' + n)!
let unmountBtn = document.getElementById('unmount' + n)!
mountBtn.addEventListener('click', () => {
let app = (apps[i] = createApp({
template: `
<ul>
<li><router-link to="/">Home</router-link></li>
<li><router-link to="/users/1">User 1</router-link></li>
<li><router-link to="/users/2">User 2</router-link></li>
</ul>
<router-view></router-view>
<router-view name="part-${n}"></router-view>
`,
}))
app.use(router)
app.provide('id', n)
app.mount('#app-' + n)
})
unmountBtn.addEventListener('click', () => {
let app = apps[i]
app && app.unmount()
})
})