NavigationFailureType 枚举:
export declare enum NavigationFailureType {
/**
* An aborted navigation is a navigation that failed because a navigation
* guard returned `false` or called `next(false)`
*/
aborted = 4,
/**
* A cancelled navigation is a navigation that failed because a more recent
* navigation finished started (not necessarily finished).
*/
cancelled = 8,
/**
* A duplicated navigation is a navigation that failed because it was
* initiated while already being at the exact same location.
*/
duplicated = 16
}
三种状态:
aborted:程序终端,导航守卫return false,或调用next(false)
cancelled: 快速切换到其他路由
duplicated: 切换到当前的路由
HistoryState:
export declare interface HistoryState {
[x: number]: HistoryStateValue;
[x: string]: HistoryStateValue;
}
该类型定义可以优化为:
export declare interface HistoryState {
[x: number | string]: HistoryStateValue
}
HistoryState 类型主要用于描述浏览器历史记录中的状态对象。当用户通过浏览器的前进/后退按钮或者通过其他方式(如程序逻辑触发的导航)在不同的路由之间导航时,Vue Router 会利用浏览器的history的 API (history.pushState, history.replaceState) 来管理这些导航。
应用:
执行一个导航时,可以通过 pushState 或 replaceState 方法传递一个 HistoryState 对象来保存一些额外的信息
beforeRouteEnter 和 beforeRouteUpdate 导航守卫来访问这些状态对象
NavigationGuardNext:
export declare interface NavigationGuardNext {
(): void;
(error: Error): void;
(location: RouteLocationRaw): void;
(valid: boolean | undefined): void;
(cb: NavigationGuardNextCallback): void;
}
next函数的类型定义,这里使用了类型重载
next可以什么都不传;可以传入Error对象;可以传入路由地址,可以传入布尔值,也可以传入一个回调函数,传入回调函数的场景很少用到。
经常使用的是next(),next(false)和next(path)
declare type NavigationGuardNextCallback = (vm: ComponentPublicInstance) => unknown;
NavigationGuardNextCallback函数的入参是vue组件实例对象。
RouteLocationOptions:
export declare interface RouteLocationOptions {
/**
* Replace the entry in the history instead of pushing a new entry
*/
replace?: boolean;
/**
* Triggers the navigation even if the location is the same as the current one.
* Note this will also add a new entry to the history unless `replace: true`
* is passed.
*/
force?: boolean;
/**
* State to save using the History API. This cannot contain any reactive
* values and some primitives like Symbols are forbidden. More info at
* https://developer.mozilla.org/en-US/docs/Web/API/History/state
*/
state?: HistoryState;
}
当使用 router.push() 或 router.replace() 时传入的对象属性,这里只包含了部分。这里定义了强制刷新,history里是否替换,以及传入state
RouterOptions: 路由实例化的配置选项
export declare interface RouterOptions extends PathParserOptions {
/**
* History implementation used by the router. Most web applications should use
* `createWebHistory` but it requires the server to be properly configured.
* You can also use a _hash_ based history with `createWebHashHistory` that
* does not require any configuration on the server but isn't handled at all
* by search engines and does poorly on SEO.
*
* @example
* ```js
* createRouter({
* history: createWebHistory(),
* // other options...
* })
* ```
*/
history: RouterHistory;
/**
* Initial list of routes that should be added to the router.
*/
routes: Readonly<RouteRecordRaw[]>;
/**
* Function to control scrolling when navigating between pages. Can return a
* Promise to delay scrolling. Check {@link ScrollBehavior}.
*
* @example
* ```js
* function scrollBehavior(to, from, savedPosition) {
* // `to` and `from` are both route locations
* // `savedPosition` can be null if there isn't one
* }
* ```
*/
scrollBehavior?: RouterScrollBehavior;
/**
* Custom implementation to parse a query. See its counterpart,
* {@link RouterOptions.stringifyQuery}.
*
* @example
* Let's say you want to use the [qs package](https://github.com/ljharb/qs)
* to parse queries, you can provide both `parseQuery` and `stringifyQuery`:
* ```js
* import qs from 'qs'
*
* createRouter({
* // other options...
* parseQuery: qs.parse,
* stringifyQuery: qs.stringify,
* })
* ```
*/
parseQuery?: typeof parseQuery;
/**
* Custom implementation to stringify a query object. Should not prepend a leading `?`.
* {@link RouterOptions.parseQuery | parseQuery} counterpart to handle query parsing.
*/
stringifyQuery?: typeof stringifyQuery;
/**
* Default class applied to active {@link RouterLink}. If none is provided,
* `router-link-active` will be applied.
*/
linkActiveClass?: string;
/**
* Default class applied to exact active {@link RouterLink}. If none is provided,
* `router-link-exact-active` will be applied.
*/
linkExactActiveClass?: string;
}
createRouter()函数传入的配置对象
Router类型:
Router类型是Vue-Router的实例类型,createRouter()函数的返回值类型
它拥有currentRoute和options属性,都是只读的
listening属性是可读写的
另外,还有方法,go 、forward 、 back、addRoute deleteRoute replace hasRoute getRoutes resolve push replace beforeEach beforeResove afterEach onError isReady install
通过useRouter()函数可以获取router,因此,可以获取当前的路由
RouteLocation:
就是Route实例对象的类型的基类型。该类型继承自_RouteLocationBase类型,_RouteLocationBase类型又继承自MatcherLocation的部分属性:
export declare interface _RouteLocationBase extends Pick<MatcherLocation, 'name' | 'path' | 'params' | 'meta'> {
/**
* The whole location including the `search` and `hash`. This string is
* percentage encoded.
*/
fullPath: string;
/**
* Object representation of the `search` property of the current location.
*/
query: LocationQuery;
/**
* Hash of the current location. If present, starts with a `#`.
*/
hash: string;
/**
* Contains the location we were initially trying to access before ending up
* on the current location.
*/
redirectedFrom: RouteLocation | undefined;
}
Pick<MatcherLocation, 'name' | 'path' | 'params' | 'meta'>
因此,该实例类型的属性有:
name path params meta fullPath query hash redirectedFrom 如图所示:
其中,matched属性是RouteLocation类型自带的:
export declare interface RouteLocation extends _RouteLocationBase {
/**
* Array of {@link RouteRecord} containing components as they were
* passed when adding records. It can also contain redirect records. This
* can't be used directly
*/
matched: RouteRecord[];
}
RouteRecord:
该类型是RouteRecordNormalized的类型别名,该类型的作用就是我们配置给路由实例Router配置的路由表:
export declare type RouteRecord = RouteRecordNormalized;
export declare interface RouteRecordNormalized {
/**
* {@inheritDoc _RouteRecordBase.path}
*/
path: _RouteRecordBase['path'];
/**
* {@inheritDoc _RouteRecordBase.redirect}
*/
redirect: _RouteRecordBase['redirect'] | undefined;
/**
* {@inheritDoc _RouteRecordBase.name}
*/
name: _RouteRecordBase['name'];
/**
* {@inheritDoc RouteRecordMultipleViews.components}
*/
components: RouteRecordMultipleViews['components'] | null | undefined;
/**
* Nested route records.
*/
children: RouteRecordRaw[];
/**
* {@inheritDoc _RouteRecordBase.meta}
*/
meta: Exclude<_RouteRecordBase['meta'], void>;
/**
* {@inheritDoc RouteRecordMultipleViews.props}
*/
props: Record<string, _RouteRecordProps>;
/**
* Registered beforeEnter guards
*/
beforeEnter: _RouteRecordBase['beforeEnter'];
/**
* Registered leave guards
*
* @internal
*/
leaveGuards: Set<NavigationGuard>;
/**
* Registered update guards
*
* @internal
*/
updateGuards: Set<NavigationGuard>;
/**
* Registered beforeRouteEnter callbacks passed to `next` or returned in guards
*
* @internal
*/
enterCallbacks: Record<string, NavigationGuardNextCallback[]>;
/**
* Mounted route component instances
* Having the instances on the record mean beforeRouteUpdate and
* beforeRouteLeave guards can only be invoked with the latest mounted app
* instance if there are multiple application instances rendering the same
* view, basically duplicating the content on the page, which shouldn't happen
* in practice. It will work if multiple apps are rendering different named
* views.
*/
instances: Record<string, ComponentPublicInstance | undefined | null>;
/**
* Defines if this record is the alias of another one. This property is
* `undefined` if the record is the original one.
*/
aliasOf: RouteRecordNormalized | undefined;
}
这里有几个比较陌生的属性:props,beforeEnter,leaveGuards, updateGuards,enterCallbacks,instances,aliasOf
需要注意的是这些属性的值的类型
NavigationGuard:
定义的是导航的路由守卫
/**
* Navigation guard. See [Navigation
* Guards](/guide/advanced/navigation-guards.md).
*/
export declare interface NavigationGuard {
(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn>;
}
RouterView:
/**
* Component to display the current route the user is at.
*/
export declare const RouterView: new () => {
$props: AllowedComponentProps & ComponentCustomProps & VNodeProps & RouterViewProps;
$slots: {
default?: (({ Component, route, }: {
Component: VNode;
route: RouteLocationNormalizedLoaded;
}) => VNode[]) | undefined;
};
};
RouterView是一个类构造函数,参数是空,并返回一个具有特定属性的对象。
$props
属性定义了 RouterView
组件可以接受的 prop 类型。
$slots: {
: 定义了 RouterView
组件的插槽(slot)结构。