-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
103 lines (99 loc) · 2.66 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SPA WC Vite Setup</title>
<style>
:root {
--text-color: black;
}
@media (prefers-color-scheme: dark) {
:root {
--text-color: white;
}
body {
background-color: black;
color: var(--text-color);
}
a {
color: #48a3ff;
}
a:visited {
color: #3398ff;
}
}
</style>
<script type="module">
window['routerLinkSetup'] = (queryTarget) => {
queryTarget.querySelectorAll('[data-route]').forEach((routerLink) => {
routerLink.addEventListener('click', (event) => {
event.preventDefault();
const path = event.target.dataset.route;
const params = event.target.dataset.routeparams;
if (params) {
window.history.pushState({}, '', `${path}?${params}`);
} else {
window.history.pushState({}, '', path);
}
route(path);
});
});
}
routerLinkSetup(document);
window.addEventListener('popstate', (event) => {
event.preventDefault();
route(window.location.pathname);
});
window['route'] = (page) => {
switch (page) {
case '/one':
import('./src/page-one/page-one').then(() => {
document.querySelector('#router-outlet').innerHTML =
'<app-page-one></app-page-one>';
});
break;
case '/two':
import('./src/page-two/page-two').then(() => {
document.querySelector('#router-outlet').innerHTML =
'<app-page-two></app-page-two>';
});
break;
default:
document.querySelector('#router-outlet').innerHTML = 'Homepage';
break;
}
}
window.onload = () => {
route(window.location.pathname);
};
</script>
</head>
<body>
<nav>
<ul>
<li>
<a href="/" data-route="/">Home</a>
</li>
<li>
<a href="/one" data-route="/one">Page One</a>
</li>
<li>
<a href="/two" data-route="/two">Page Two</a>
</li>
</ul>
</nav>
<hr />
<div id="router-outlet"></div>
<hr />
Code:
<a href="https://github.com/LeonEck/spa-wc-vite"
>https://github.com/LeonEck/spa-wc-vite</a
>
<br />
Blog:
<a href="https://leoneck.de/blog/spa-wc-vite"
>https://leoneck.de/blog/spa-wc-vite</a
>
</body>
</html>