forked from easyroute-router/svelte-easyroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouterLink.svelte
48 lines (44 loc) · 1.23 KB
/
RouterLink.svelte
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
<div class="router-link" on:click={navigateRouter}>
<a href={(window.routermode == 'hash' ? '/#' : '')+to} on:click={preventClickLink}>
{#if text && text !== ""}
{text}
{/if}
<slot></slot>
</a>
</div>
<script>
export let to
export let text
function preventClickLink(e) {
e.preventDefault()
e.stopPropagation()
navigateRouter()
}
function navigateRouter() {
if (window.routermode == 'hash') window.location.hash = to
if (window.routermode == 'history') {
let stateObj = { path: to, needAddBase: true };
let event = new CustomEvent('svelteEasyrouteLinkClicked',
{
'detail': stateObj
});
window.dispatchEvent(event)
}
if (window.routermode == 'silent') {
let stateObj = { path: to, needAddBase: true };
let event = new CustomEvent('svelteEasyrouteSilentNavigated',
{
'detail': stateObj
});
window.dispatchEvent(event)
}
}
</script>
<style>
div.router-link {
color: blue;
text-decoration: underline;
cursor: pointer;
display: inline-block;
}
</style>