UNPKG

@ljweb/uniapp-router-next

Version:

> 一个类似于vue-router的路由器,用于uniapp(vue3),支持h5和微信小程序和app,其他小程序请自测

462 lines (375 loc) 13.9 kB
import type { App } from 'vue'; import type { InjectionKey } from 'vue'; import type { Ref } from 'vue'; import type { RouteMeta as RouteMeta_2 } from 'vue-router'; import type { Router as Router_2 } from 'vue-router'; import type { RouterScrollBehavior } from 'vue-router'; /** * APP配置 */ export declare interface AppConfig { } /** * 路由小程序配置 */ export declare interface AppletConfig { } /** * 创建路由匹配器 */ declare function createRouteMatcher(options: RouterOptions): { getRouteByAliasPath: (aliasPath: string) => RouteRecordRaw | undefined; getRouteByPath: (path: string) => RouteRecordRaw | undefined; getRouteByName: (name: string) => RouteRecordRaw | undefined; }; export declare function createRouter(options: RouterOptions): Router; export declare function createRouterError<E extends NavigationFailure | MatcherFailure>(type: E['type'], params: Omit<E, 'type' | keyof Error>): E; /** * Decode text using `decodeURIComponent`. Returns the original text if it * fails. * * @param text - string to decode * @returns decoded string */ export declare function decode(text: string | number): string; export declare function decodeQuery(query: LocationQuery): LocationQuery; /** * Encode characters that need to be encoded on the hash section of the URL. * * @param text - string to encode * @returns encoded string */ export declare function encodeHash(text: string): string; /** * Encode characters that need to be encoded on the path section of the URL as a * param. This function encodes everything {@link encodePath} does plus the * slash (`/`) character. If `text` is `null` or `undefined`, returns an empty * string instead. * * @param text - string to encode * @returns encoded string */ export declare function encodeParam(text: string | number | null | undefined): string; /** * Encode characters that need to be encoded on the path section of the URL. * * @param text - string to encode * @returns encoded string */ export declare function encodePath(text: string | number): string; /** * Like `encodeQueryValue` but also encodes the `=` character. * * @param text - string to encode */ export declare function encodeQueryKey(text: string | number): string; /** * Encode characters that need to be encoded query values on the query * section of the URL. * * @param text - string to encode * @returns encoded string */ export declare function encodeQueryValue(text: string | number): string; export declare function error(msg: string): void; export declare function getEnterRoute(router: Router): RouteLocationPathRaw; export declare type GuardLocation = string | RouteLocationPathRaw | RouteLocationNameRaw; export declare type GuardName = 'beforeGuards' | 'afterGuards'; export declare type Guards = Record<GuardName, ReturnType<typeof useCallbacks<NavigationGuard> | typeof useCallbacks<NavigationHookAfter>> | ReturnType<typeof useCallbacks<any>>>; /** * 1. 放回false取消导航 */ export declare function guardToPromiseFn(guard: NavigationGuard, to: RouteLocationNormalized, from: RouteLocationNormalized): () => Promise<void>; /** * 路由h5配置,参考vue-router */ export declare interface H5Config { linkActiveClass?: string; linkExactActiveClass?: string; scrollBehavior?: RouterScrollBehavior; } export declare const isArray: (arg: any) => arg is any[]; export declare const isDate: (val: unknown) => val is Date; export declare const isFunction: (val: unknown) => val is Function; export declare const isMap: (val: unknown) => val is Map<any, any>; export declare function isNavigationFailure(error: any, type?: number): error is NavigationFailure; export declare const isObject: (val: unknown) => val is Record<any, any>; export declare const isRegExp: (val: unknown) => val is RegExp; export declare const isSet: (val: unknown) => val is Set<any>; export declare const isString: (val: unknown) => val is string; export declare const isSymbol: (val: unknown) => val is symbol; export declare interface LocationNormalized extends Pick<RouteLocationNormalized, 'path' | 'query'> { } export declare type LocationQuery = Record<string, LocationQueryValue>; export declare type LocationQueryValue = string | number | null; export declare interface MatcherFailure extends Error { type: NavigationErrorTypes.MATCHER_NOT_FOUND; location: RouteLocationRaw; } export declare const mpPlatformReg: RegExp; declare interface NavigateToSuccessOptions { /** * 回调信息 */ errMsg: string; /** * 和被打开页面进行通信 */ eventChannel: any; } export declare const enum NavigationErrorTypes { /** * 找不到路由 */ MATCHER_NOT_FOUND = 1, /** * 重定向 */ NAVIGATION_GUARD_REDIRECT = 2, /** * 终止导航 */ NAVIGATION_ABORTED = 4, /** * 导航取消 */ NAVIGATION_CANCELLED = 8, /** * 导航重复 */ NAVIGATION_DUPLICATED = 16 } export declare interface NavigationFailure extends Error { type: NavigationErrorTypes.NAVIGATION_CANCELLED | NavigationErrorTypes.NAVIGATION_ABORTED | NavigationErrorTypes.NAVIGATION_DUPLICATED | NavigationErrorTypes.NAVIGATION_GUARD_REDIRECT; from: RouteLocationNormalized; to: RouteLocationNormalized; } export declare interface NavigationGuard { (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn>; } export declare type NavigationGuardNext = (valid?: NavigationGuardReturn) => void; export declare type NavigationGuardReturn = void | Error | RouteLocationPathRaw | RouteLocationNameRaw | boolean | string; export declare interface NavigationHookAfter { (to: RouteLocationNormalized, from: RouteLocationNormalized): any; } export declare type NavigationReturn = UniNavigationReturn | NavigationFailure | void | undefined | MatcherFailure; /** * 导航类型 */ export declare type NavigationType = 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab' | 'navigateBack'; export declare enum NavigationTypesEnums { 'navigate' = "navigateTo", 'redirect' = "redirectTo", 'reLaunch' = "reLaunch", 'switchTab' = "switchTab", 'navigateBack' = "navigateBack" } export declare const objectToString: () => string; export declare function parseQuery(search: string): LocationQuery; export declare function parseURL(parseQuery: (search: string) => LocationQuery, location: string, currentLocation?: string): LocationNormalized; /** * 平台类型 */ export declare type PlatformType = 'h5' | 'mp-weixin' | 'mp-alipay' | 'app' | 'mp-baidu' | 'mp-qq' | 'mp-toutiao' | 'mp-lark' | 'mp-kuaishou' | 'mp-jd'; export declare const PLUS_RE: RegExp; export declare function resolveRelativePath(to: string, from: string): string; export declare interface RouteLocation extends RouteLocationOptions { delta?: number; path: string; fullPath: string; query: LocationQuery; meta: RouteMeta; redirect?: RouteRedirect; name: RouteRecordName; [propName: string]: any; } export declare interface RouteLocationBack extends Omit<RouteLocationBackRaw, 'query'> { } export declare interface RouteLocationBackRaw extends RouteLocationOptions { delta: number; } export declare interface RouteLocationFrom extends RouteLocationFromRaw { } /** * @description h5端点击自带头部返回和底部导航时的参数 * @export * @interface RouteLocationFromRaw */ export declare interface RouteLocationFromRaw extends RouteLocationOptions { from: 'backbutton' | 'tabbar'; url?: string; } export declare const routeLocationKey: InjectionKey<RouteLocationNormalized>; export declare interface RouteLocationNameRaw extends RouteLocationOptions { name: RouteRecordName; } export declare interface RouteLocationNormalized { path: string; fullPath: string; query: LocationQuery; meta: RouteMeta; name: RouteRecordName; [propName: string]: any; } export declare interface RouteLocationOptions { query?: LocationQuery; /** *是否开启强制跳转,强制跳转将忽略相同路由的跳转,以及路由锁 */ force?: boolean; /** * 窗口动画,app端有效 */ animationType?: string; /** * 窗口动画持续时间,单位为 ms,默认300 */ animationDuration?: number; /** * 跳转类型 */ navType?: NavigationType; /** * 页面间通信接口,用于监听被打开页面发送到当前页面的数据 */ events?: any; /** * 接口调用成功的回调函数 */ success?: (result: NavigateToSuccessOptions) => void; /** * 接口调用失败的回调函数 */ fail?: (result: any) => void; /** * 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: (result: any) => void; } export declare interface RouteLocationPathRaw extends RouteLocationOptions { path: string; } export declare type RouteLocationRaw = string | RouteLocationPathRaw | RouteLocationNameRaw | RouteLocationUrlRaw | RouteLocationFromRaw | RouteLocationBackRaw; export declare interface RouteLocationUrlRaw extends RouteLocationOptions { url: string; } declare type RouteMatcher = ReturnType<typeof createRouteMatcher>; export declare interface RouteMeta extends RouteMeta_2 { } export declare interface Router { level: number; lock: boolean; fromRoute?: RouteLocationNormalized; currentRoute: Ref<RouteLocationNormalized>; vueRouter: Router_2 | null; readonly options: RouterOptions; readonly guards: Guards; routeMatcher: RouteMatcher; resolve(rawLocation: RouteLocationRaw, navType?: NavigationType): RouteLocation | undefined; navigateTo(to: RouteLocationRaw): Promise<NavigationReturn>; jump(to: RouteLocationRaw, navType: NavigationType): Promise<NavigationReturn>; navigate(to: RouteLocationRaw, navType?: NavigationType, redirectedFrom?: RouteLocation): Promise<NavigationReturn>; switchTab(to: RouteLocationRaw): Promise<NavigationReturn>; redirectTo(to: RouteLocationRaw): Promise<NavigationReturn>; reLaunch(to: RouteLocationRaw): Promise<NavigationReturn>; navigateBack(to?: RouteLocationBackRaw): Promise<NavigationReturn>; forceGuardEach(): Promise<NavigationReturn>; beforeEach(guard: NavigationGuard): void; afterEach(guard: NavigationHookAfter): void; install(app: App<any>): any; parseQuery: typeof parseQuery; stringifyQuery: typeof stringifyQuery; } export declare type RouteRecordName = string | null | undefined; export declare interface RouteRecordRaw { path: string; meta?: RouteMeta; name?: RouteRecordName; redirect?: RouteRedirect; [propName: string]: any; } export declare type RouteRedirect = RouteLocationRaw | ((to: RouteLocationRaw) => RouteLocationRaw); export declare const routerKey: InjectionKey<Router>; export declare interface RouterOptions { /** * 当前运行平台 */ platform: PlatformType; /** * debug模式,开启可以查看打印日志 */ debug?: boolean; /** * h5端配置 */ h5?: H5Config; /** * 小程序配置 */ applet?: AppletConfig; /** * APP配置 */ app?: AppConfig; /** * 解析路由参数 */ parseQuery?: typeof parseQuery; /** * 解析路由参数字符串转对象 */ stringifyQuery?: typeof stringifyQuery; /** * 路由集 */ routes: RouteRecordRaw[]; } export declare function runGuardQueue(guards: (() => Promise<any>)[]): Promise<void>; /** * 初始路由 */ export declare const START_LOCATION_NORMALIZED: RouteLocationNormalized; export declare function stringifyQuery(query: LocationQuery): string; export declare function stringifyURL(stringifyQuery: (query: LocationQuery) => string, location: LocationNormalized): string; export declare const toTypeString: (value: unknown) => string; declare interface UniNavigationReturn { /** * 回调信息 */ errMsg: string; /** * 和被打开页面进行通信 */ eventChannel?: any; } export declare type UniNavigationType = 'navigate' | 'redirect' | 'reLaunch' | 'switchTab' | 'navigateBack'; export declare const uniRouterApiName: string[]; export declare function useCallbacks<T>(): { add: (handler: T) => () => void; list: () => T[]; reset: () => void; }; export declare type UserGuard = NavigationGuard | NavigationHookAfter; export declare function useRoute(): RouteLocationNormalized; export declare function useRouter(): Router; export declare function warn(msg: string, debug?: boolean, ...args: any[]): void; export declare const WILDCARD = "*"; export declare const WILDCARD_ROUTE: { path: string; }; export { } // extend vue declare module 'vue' { export interface ComponentCustomProperties { $uniRoute: RouteLocationNormalized $uniRouter: Router } export interface GlobalComponents { // eslint-disable-next-line @typescript-eslint/consistent-type-imports RouterNavigate: import('vue').DefineComponent<{ to?: RouteLocationRaw navType?: 'navigate' | 'redirect' | 'reLaunch' | 'switchTab' | 'navigateBack' delta?: number }> } }