Skip to content

Commit 2dac0eb

Browse files
committed
feat: export RouterLink RouterView
1 parent 1e4b9cb commit 2dac0eb

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* @flow */
22

3+
import RouterView from './components/view'
4+
import RouterLink from './components/link'
35
import { install } from './install'
46
import { START } from './util/route'
57
import { assert, warn } from './util/warn'
@@ -24,6 +26,8 @@ export default class VueRouter {
2426
static isNavigationFailure: Function
2527
static NavigationFailureType: any
2628
static START_LOCATION: Route
29+
static RouterView: any
30+
static RouterLink: any
2731

2832
app: any
2933
apps: Array<any>
@@ -283,6 +287,8 @@ function createHref (base: string, fullPath: string, mode) {
283287
return base ? cleanPath(base + '/' + path) : path
284288
}
285289

290+
VueRouter.RouterView = RouterView
291+
VueRouter.RouterLink = RouterLink
286292
VueRouter.install = install
287293
VueRouter.version = '__VERSION__'
288294
VueRouter.isNavigationFailure = isNavigationFailure

types/RouterLink.d.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { VNode } from 'vue'
2+
import { NavigationFailure, Route } from './router'
3+
4+
export interface RouterLinkProps {
5+
/**
6+
* Denotes the target route of the link. When clicked, the value of the `to` prop will be passed to `router.push()` internally, so the value can be either a string or a location descriptor object.
7+
*/
8+
to: string | Location
9+
/**
10+
* Setting `replace` prop will call `router.replace()` instead of `router.push()` when clicked, so the navigation will not leave a history record.
11+
*
12+
* @default false
13+
*/
14+
replace?: boolean
15+
/**
16+
* Setting `append` prop always appends the relative path to the current path. For example, assuming we are navigating from `/a` to a relative link `b`, without `append` we will end up at `/b`, but with append we will end up at `/a/b`.
17+
*
18+
* @default false
19+
*/
20+
append?: boolean
21+
/**
22+
* Sometimes we want <RouterLink> to render as another tag, e.g <li>. Then we can use tag prop to specify which tag to render to, and it will still listen to click events for navigation.
23+
*
24+
* @default "a"
25+
*/
26+
tag?: string
27+
/**
28+
* Configure the active CSS class applied when the link is active. Note the default value can also be configured globally via the `linkActiveClass` router constructor option.
29+
*
30+
* @default "router-link-active"
31+
*/
32+
activeClass?: string
33+
/**
34+
* The default active class matching behavior is **inclusive match**. For example, `<RouterLink to="/a">` will get this class applied as long as the current path starts with `/a/` or is `/a`.
35+
*
36+
* @default false
37+
*/
38+
exact?: boolean
39+
/**
40+
* Allows matching only using the `path` section of the url, effectively ignoring the `query` and the `hash` sections.
41+
*
42+
* @default false
43+
*/
44+
exactPath?: boolean
45+
/**
46+
* Configure the active CSS class applied when the link is active with exact path match. Note the default value can also be configured globally via the `linkExactPathActiveClass` router constructor option.
47+
*
48+
* @default "router-link-exact-path-active"
49+
*/
50+
exactPathActiveClass?: string
51+
52+
/**
53+
* Specify the event(s) that can trigger the link navigation.
54+
*
55+
* @default 'click'
56+
*/
57+
event?: string | ReadonlyArray<string>
58+
/**
59+
* Configure the active CSS class applied when the link is active with exact match. Note the default value can also be configured globally via the `linkExactActiveClass` router constructor option.
60+
*
61+
* @default "router-link-exact-active"
62+
*/
63+
exactActiveClass?: string
64+
/**
65+
* Configure the value of `aria-current` when the link is active with exact match. It must be one of the allowed values for [aria-current](https://www.w3.org/TR/wai-aria-1.2/#aria-current) in the ARIA spec. In most cases, the default of page should be the best fit.
66+
*
67+
* @default "page"
68+
*/
69+
ariaCurrentValue?:
70+
| 'page'
71+
| 'step'
72+
| 'location'
73+
| 'date'
74+
| 'time'
75+
| 'true'
76+
| 'false'
77+
}
78+
79+
interface RouterLinkSlotArgument {
80+
/**
81+
* resolved url. This would be the `href` attribute of an `a` element
82+
*/
83+
href: string
84+
/**
85+
* resolved normalized location
86+
*/
87+
route: Route
88+
/**
89+
* function to trigger the navigation. It will automatically prevent events when necessary, the same way `RouterLink` does
90+
*/
91+
navigate: (e?: MouseEvent) => Promise<undefined | NavigationFailure>
92+
/**
93+
* `true` if the [active class](https://router.vuejs.org/api/#active-class) should be applied. Allows to apply an arbitrary class
94+
*/
95+
isActive: boolean
96+
/**
97+
* `true` if the [exact active class](https://router.vuejs.org/api/#exact-active-class) should be applied. Allows to apply an arbitrary class
98+
*/
99+
isExactActive: boolean
100+
}
101+
102+
/**
103+
* Component to render a link that triggers a navigation on click.
104+
*/
105+
export declare const RouterLink: new (props: RouterLinkProps) => {
106+
$props: typeof props
107+
$scopedSlots: {
108+
default?:
109+
| undefined
110+
| (({
111+
href,
112+
route,
113+
navigate,
114+
isActive,
115+
isExactActive
116+
}: RouterLinkSlotArgument) => VNode[] | undefined)
117+
}
118+
}

types/RouterView.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface RouterViewProps {
2+
/**
3+
* When a <RouterView> has a name, it will render the component with the corresponding name in the matched route record's components option. See [Named Views](https://router.vuejs.org/guide/essentials/named-views.html) for an example.
4+
*
5+
* @default "default"
6+
*/
7+
name?: string
8+
}
9+
10+
/**
11+
* Component to display the current route the user is at.
12+
*/
13+
export declare const RouterView: new (props: RouterViewProps) => {
14+
$props: typeof props
15+
}

types/router.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import Vue, { ComponentOptions, PluginFunction, AsyncComponent } from 'vue'
1+
import Vue, { ComponentOptions, PluginFunction, AsyncComponent } from 'vue'
2+
import { RouterLink } from './RouterLink'
3+
import { RouterView } from './RouterView'
24

35
type Component = ComponentOptions<Vue> | typeof Vue | AsyncComponent
46
type Dictionary<T> = { [key: string]: T }
@@ -78,6 +80,16 @@ export declare class VueRouter {
7880
}
7981

8082
static START_LOCATION: Route
83+
84+
/**
85+
* Component to render a link that triggers a navigation on click.
86+
*/
87+
static readonly RouterLink: typeof RouterLink
88+
89+
/**
90+
* Component to display the current route the user is at.
91+
*/
92+
static readonly RouterView: typeof RouterView
8193
}
8294

8395
export enum NavigationFailureType {

0 commit comments

Comments
 (0)