@esmx/router-vue 3.0.0-rc.103
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +570 -0
- package/README.zh-CN.md +570 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +13 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.mjs +216 -0
- package/dist/plugin.d.ts +61 -0
- package/dist/plugin.mjs +41 -0
- package/dist/plugin.test.d.ts +1 -0
- package/dist/plugin.test.mjs +631 -0
- package/dist/router-link.d.ts +220 -0
- package/dist/router-link.mjs +119 -0
- package/dist/router-link.test.d.ts +1 -0
- package/dist/router-link.test.mjs +663 -0
- package/dist/router-view.d.ts +31 -0
- package/dist/router-view.mjs +15 -0
- package/dist/router-view.test.d.ts +1 -0
- package/dist/router-view.test.mjs +676 -0
- package/dist/run-with-context.test.d.ts +1 -0
- package/dist/run-with-context.test.mjs +57 -0
- package/dist/use.d.ts +260 -0
- package/dist/use.mjs +125 -0
- package/dist/use.test.d.ts +1 -0
- package/dist/use.test.mjs +381 -0
- package/dist/util.d.ts +20 -0
- package/dist/util.mjs +49 -0
- package/dist/util.test.d.ts +4 -0
- package/dist/util.test.mjs +604 -0
- package/dist/vue2.d.ts +15 -0
- package/dist/vue2.mjs +0 -0
- package/dist/vue3.d.ts +13 -0
- package/dist/vue3.mjs +0 -0
- package/package.json +85 -0
- package/src/index.test.ts +273 -0
- package/src/index.ts +15 -0
- package/src/plugin.test.ts +812 -0
- package/src/plugin.ts +107 -0
- package/src/router-link.test.ts +830 -0
- package/src/router-link.ts +172 -0
- package/src/router-view.test.ts +840 -0
- package/src/router-view.ts +59 -0
- package/src/run-with-context.test.ts +64 -0
- package/src/use.test.ts +484 -0
- package/src/use.ts +416 -0
- package/src/util.test.ts +760 -0
- package/src/util.ts +85 -0
- package/src/vue2.ts +18 -0
- package/src/vue3.ts +15 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { RouterLinkProps } from '@esmx/router';
|
|
2
|
+
import { defineComponent, h, type PropType } from 'vue';
|
|
3
|
+
import { useLink } from './use';
|
|
4
|
+
import { isVue2 } from './util';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* RouterLink component for navigation.
|
|
8
|
+
* Renders an anchor tag with proper navigation behavior and active state management.
|
|
9
|
+
*
|
|
10
|
+
* @param props - Component properties
|
|
11
|
+
* @param props.to - Target route location to navigate to
|
|
12
|
+
* @param props.type - Navigation type ('push' | 'replace' | 'pushWindow' | 'replaceWindow' | 'pushLayer')
|
|
13
|
+
* @param props.replace - Use type='replace' instead
|
|
14
|
+
* @param props.exact - How to match the active state ('include' | 'exact' | 'route')
|
|
15
|
+
* @param props.activeClass - CSS class to apply when link is active
|
|
16
|
+
* @param props.event - Event(s) that trigger navigation
|
|
17
|
+
* @param props.tag - Custom tag to render instead of 'a'
|
|
18
|
+
* @param props.layerOptions - Layer options for layer-based navigation
|
|
19
|
+
* @param slots - Component slots
|
|
20
|
+
* @param slots.default - Default slot content
|
|
21
|
+
* @returns Vue component instance
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```vue
|
|
25
|
+
* <template>
|
|
26
|
+
* <nav>
|
|
27
|
+
* <!-- Basic navigation -->
|
|
28
|
+
* <RouterLink to="/home">Home</RouterLink>
|
|
29
|
+
* <RouterLink to="/about">About</RouterLink>
|
|
30
|
+
*
|
|
31
|
+
* <!-- With custom styling -->
|
|
32
|
+
* <RouterLink
|
|
33
|
+
* to="/dashboard"
|
|
34
|
+
* active-class="nav-active"
|
|
35
|
+
* >
|
|
36
|
+
* Dashboard
|
|
37
|
+
* </RouterLink>
|
|
38
|
+
*
|
|
39
|
+
* <!-- Replace navigation -->
|
|
40
|
+
* <RouterLink to="/login" type="replace">Login</RouterLink>
|
|
41
|
+
*
|
|
42
|
+
* <!-- Custom tag and exact matching -->
|
|
43
|
+
* <RouterLink
|
|
44
|
+
* to="/contact"
|
|
45
|
+
* exact="exact"
|
|
46
|
+
* tag="button"
|
|
47
|
+
* class="btn"
|
|
48
|
+
* >
|
|
49
|
+
* Contact
|
|
50
|
+
* </RouterLink>
|
|
51
|
+
* </nav>
|
|
52
|
+
* </template>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export const RouterLink = defineComponent({
|
|
56
|
+
name: 'RouterLink',
|
|
57
|
+
props: {
|
|
58
|
+
/**
|
|
59
|
+
* Target route location to navigate to.
|
|
60
|
+
* Can be a string path or route location object.
|
|
61
|
+
* @example '/home' | { path: '/user', query: { id: '123' } }
|
|
62
|
+
*/
|
|
63
|
+
to: {
|
|
64
|
+
type: [String, Object] as PropType<RouterLinkProps['to']>,
|
|
65
|
+
required: true
|
|
66
|
+
},
|
|
67
|
+
/**
|
|
68
|
+
* Navigation type for the link.
|
|
69
|
+
* @default 'push'
|
|
70
|
+
* @example 'push' | 'replace' | 'pushWindow' | 'replaceWindow' | 'pushLayer'
|
|
71
|
+
*/
|
|
72
|
+
type: {
|
|
73
|
+
type: String as PropType<RouterLinkProps['type']>,
|
|
74
|
+
default: 'push'
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* @deprecated Use 'type="replace"' instead
|
|
78
|
+
* @example :replace={true} → type="replace"
|
|
79
|
+
*/
|
|
80
|
+
replace: {
|
|
81
|
+
type: Boolean as PropType<RouterLinkProps['replace']>,
|
|
82
|
+
default: false
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* How to match the active state.
|
|
86
|
+
* - 'include': Match if current route includes this path
|
|
87
|
+
* - 'exact': Match only if routes are exactly the same
|
|
88
|
+
* - 'route': Match based on route configuration
|
|
89
|
+
* @default 'include'
|
|
90
|
+
*/
|
|
91
|
+
exact: {
|
|
92
|
+
type: String as PropType<RouterLinkProps['exact']>,
|
|
93
|
+
default: 'include'
|
|
94
|
+
},
|
|
95
|
+
/**
|
|
96
|
+
* CSS class to apply when link is active (route matches).
|
|
97
|
+
* @example 'nav-active' | 'selected'
|
|
98
|
+
*/
|
|
99
|
+
activeClass: {
|
|
100
|
+
type: String as PropType<RouterLinkProps['activeClass']>
|
|
101
|
+
},
|
|
102
|
+
/**
|
|
103
|
+
* Event(s) that trigger navigation. Can be string or array of strings.
|
|
104
|
+
* @default 'click'
|
|
105
|
+
* @example 'click' | ['click', 'mouseenter']
|
|
106
|
+
*/
|
|
107
|
+
event: {
|
|
108
|
+
type: [String, Array] as PropType<RouterLinkProps['event']>,
|
|
109
|
+
default: 'click'
|
|
110
|
+
},
|
|
111
|
+
/**
|
|
112
|
+
* Custom tag to render instead of 'a'.
|
|
113
|
+
* @default 'a'
|
|
114
|
+
* @example 'button' | 'div' | 'span'
|
|
115
|
+
*/
|
|
116
|
+
tag: { type: String as PropType<RouterLinkProps['tag']>, default: 'a' },
|
|
117
|
+
/**
|
|
118
|
+
* Layer options for layer-based navigation.
|
|
119
|
+
* Only used when type='pushLayer'.
|
|
120
|
+
* @example { zIndex: 1000, autoPush: false, routerOptions: { mode: 'memory' } }
|
|
121
|
+
*/
|
|
122
|
+
layerOptions: {
|
|
123
|
+
type: Object as PropType<RouterLinkProps['layerOptions']>
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* Custom navigation handler called before navigation.
|
|
127
|
+
* Receives the event object and the event name that triggered navigation.
|
|
128
|
+
*
|
|
129
|
+
* @Note you need to call `e.preventDefault()` to prevent default browser navigation.
|
|
130
|
+
*/
|
|
131
|
+
beforeNavigate: {
|
|
132
|
+
type: Function as PropType<RouterLinkProps['beforeNavigate']>
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
setup(props, context) {
|
|
137
|
+
const link = useLink(props);
|
|
138
|
+
|
|
139
|
+
if (isVue2) {
|
|
140
|
+
return () => {
|
|
141
|
+
const { class: className, ...attributes } =
|
|
142
|
+
link.value.attributes;
|
|
143
|
+
return h(
|
|
144
|
+
link.value.tag,
|
|
145
|
+
{
|
|
146
|
+
attrs: {
|
|
147
|
+
...attributes,
|
|
148
|
+
...context.attrs
|
|
149
|
+
},
|
|
150
|
+
class: className,
|
|
151
|
+
on: link.value.createEventHandlers()
|
|
152
|
+
},
|
|
153
|
+
context.slots.default?.()
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
return () => {
|
|
158
|
+
return h(
|
|
159
|
+
link.value.tag,
|
|
160
|
+
{
|
|
161
|
+
...link.value.attributes,
|
|
162
|
+
...context.attrs,
|
|
163
|
+
...link.value.createEventHandlers(
|
|
164
|
+
(name) =>
|
|
165
|
+
`on${name.charAt(0).toUpperCase()}${name.slice(1)}`
|
|
166
|
+
)
|
|
167
|
+
},
|
|
168
|
+
context.slots.default?.()
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
});
|