Skip to content

常用类型约束

在该章节的文档中,我们只列举了一部分常用的 TypeScript 声明类型,以便帮助您更好地理解和使用 uni-simple-router。如果您需要查看全部的 TypeScript 声明类型,您可以在插件包的根目录下找到 index.d.ts 文件。

dynamicImportComponentRule

ts
type dynamicImportComponentRule = (path: string, optios?: {
    pageType?: `top` | '';
    style?: Record<string, any>;
    package?: {
        type?: `main` | `child`;
        name?: string;
    };
}) => RouteRecordComponentRule;

如需查阅所有字段说明,你可以在此处找到 dynamicImportComponentRule

RouterRule

ts
interface RouterRule {
    initOptions: InitRouterOptionsRule;
    initEnvOptions: {
        matcher: MatcherRule;
        vueRouter: Router | null;
        compileOptions: CompileAppOptionsRule;
        h5UniRoutes: Array<{
            path: string;
            alias?: string;
            name?: string;
            component: {
                setup: () => void;
            };
            loader: any;
            meta: Record<string, any>;
            children?: RouteRecordBaseRule[];
        }>;
        beforeGuards: CallBackOptions<NavigationGuardWithThis>;
        afterGuards: CallBackOptions<NavigationHookAfter>;
        resolveGuards: CallBackOptions<NavigationGuardWithThis>;
        errorHandlers: CallBackOptions<_ErrorHandler>;
        uniMethods: {
            navigateTo: RouterRule[`push`];
            redirectTo: RouterRule[`replace`];
            reLaunch: RouterRule[`replaceAll`];
            switchTab: RouterRule[`pushTab`];
            navigateBack: RouterRule[`back`];
        };
    };
    expandOptions: {
        currentRouteMap: RouteRecordRowMap;
        externalHookMethodsMap: Record<ExternalHookMethodName, ExternalHookMethodsRule[]>;
        waitRunHooks: Array<{
            runOriginalHook: () => void;
            hookName: OptionHookNameRule | PositionHookNameRule;
        }>;
        restoreHokHooks: Array<() => void>;
    };
    history: AbstractHistory;
    currentRoute: ShallowRef<RouteRecordRow>;
    abstractRoutes: ShallowRef<(Record<number, RouteRecordRow>)>;
    $lockStatus: boolean;
    _runtimeId: number;
    _launched: boolean;
    _patch: {
        getCurrentPages?(): ComponentPublicInstance[];
        _ksFirstRedirect: boolean;
    };
    getRouterView(): {
        uniSimpleRouterView: App_2;
    };
    externalMethodToCache(router: RouterRule, options: ExternalHookMethodsRule): void;
    beforeEach(guard: NavigationGuardWithThis): () => void;
    afterEach(guard: NavigationHookAfter): () => void;
    beforeResolve(guard: NavigationGuardWithThis): () => void;
    installRouter(router: RouterRule, app: App_2, options: CompileAppOptionsRule): void;
    push(to: RoutePushLocationRaw): Promise<void | NavigationFailure | undefined>;
    pushTab(to: RouteLocationRaw): Promise<void | NavigationFailure | undefined>;
    replace(to: RouteLocationRaw): Promise<void | NavigationFailure | undefined>;
    replaceAll(to: RouteLocationRaw): Promise<void | NavigationFailure | undefined>;
    back(to: NavigateBackRaw | number | undefined): Promise<void | NavigationFailure | undefined>;
    onError(handler: _ErrorHandler): () => void;
    addRoute(parentOrRoute: RouteRecordName | RouteRecordBaseRule, route?: RouteRecordBaseRule): () => boolean;
    removeRoute(matcherRef: RouteRecordName | RouteRecordMatcher): boolean;
    utils: {
        locationAppHome: (router: RouterRule) => never | (() => Promise<PushWithRedirectRule>);
        pagesPathIsTabbar: (pagesPath: string) => boolean;
        appletMixins: (router: RouterRule, app: App_2, rootComponent: Record<string, (() => void) | undefined>) => void;
        setComponentInstances: (router: RouterRule, componentId: number, instances: InstancesRule) => void;
        removeComponentInstances: (router: RouterRule, componentId: number, instanceName: string) => true;
    };
}

InitRouterOptionsRule

ts
interface InitRouterOptionsRule {
    /**
     * 路由表
     */
    routes: Readonly<RouteRecordBaseRule[]>;
    /**
     *  运行平台
     */
    platform: Readonly<platformRule>;
    /**
     * 是否保留uni-app的原始跳转方法
     * 保留的情况下 执行uni-app的跳转方法 不会触发路由跳转
     */
    keepUniOriginNav?: boolean;
    /**
     * api 返回类型
     */
    backType?: backTypeRule;
    /**
     * app或者小程序端在启动时
     * 启动失败的生命周期
     */
    launchedFail?: (err: NavigationFailure) => void;
    /**
     * 路由表未匹配到时触发该函数
     */
    routeNotFound?: (to: Record<string, any>) => RouteLocationRaw;
    /**
     * 开发环境下 非H5端热编译导致嵌套视图路径丢失的问题
     */
    hotRefresh?: {
        mode: `development` | `production`;
        to: RoutePushLocationRaw;
        navType: NavTypeRule;
    };
    /**
     * 路由锁配置
     */
    navigateLock?: {
        /**
         * 自动解锁状态集合 包含该状态时会自动解锁
         */
        autoUnlock?: NavigationFailureType[];
        /**
         * 路由锁 解锁函数 无论如何都会执行
         */
        detectBeforeLock?: (router: RouterRule, to: ToLocationRawRule, navType: NavTypeRule) => void;
    };
    /**
     * app 端专属配置
     */
    app?: {
        /**
         * app 端退出应用函数 如果默认不传递则走插件底层函数
         */
        runtimeQuit?: (title?: string) => void;
        /**
         * app 启动完成后会触发该函数
         */
        launchedHook?: () => void;
        /**
         * app 设置侧滑手势 仅ios支持
         */
        setSideSlipGesture?: (to: RouteRecordRow) => iosPopGestureRule;
    };
    /**
     * 小程序 端专属配置
     */
    applet?: {
        /**
         * 小程序 启动完成后会触发该函数
         */
        launchedHook?: () => void;
        /**
         * 是否自动隐藏原生导航栏小房子按钮
         */
        hideHomeButton?: boolean;
    };
    /**
     * H5 端专属配置
     */
    h5?: {
        /**
         * 切换页面的动画配置
         */
        animation?: {
            /**
             * 需要监听的跳转类型
             */
            includeNavtype: NavTypeRule[];
            /***
             * 动画配置
             */
            animationNodeMap: H5AnimationNodeMapRule;
            /**
             * 动画时长
             */
            animationTime: number;
            /**
             * 默认动画
             */
            defaultAnimationType: keyof H5AnimationNodeMapRule | ((to: RouteRecordRow, from: RouteRecordRow) => keyof H5AnimationNodeMapRule | null);
            /**
             * 动画窗口根节点
             */
            animationAppEl: HTMLElement | ((to: RouteRecordRow, from: RouteRecordRow) => HTMLElement | null) | string;
            /**
             * 动画创建之前的函数
             */
            beforeCreate?: (config: ProxyAnimationRule, to: RouteRecordRow, from: RouteRecordRow) => void | ProxyAnimationRule;
            /**
             * 动画完成之后
             */
            afterFinish?: (to: RouteRecordRow, from: RouteRecordRow) => void;
        };
    };
}

DynamicRouteMatchRule

ts
interface DynamicRouteMatchRule {
    path: string;
    name: string;
    params: Record<string, any>;
    query: Record<string, any>;
}

H5AnimationNodeMapRule

ts
/**
 * h5 动画配置节点
 */
interface H5AnimationNodeMapRule {
    [propName: string]: [
    /**
    * 前进时触发的动画规则
    * 数组第一位为新开窗口动画
    * 数组第一位为底层旧窗口动画
    */
    [
    string,
    string | undefined
    ],
    /**
    * 后退时触发的动画规则
    * 数组第一位为新开窗口动画
    * 数组第一位为底层旧窗口动画
    */
    [
    string,
    string | undefined
    ] | undefined
    ];
}

iosPopGestureRule

ts
/**
 * ios 上的侧滑手势
 */
type iosPopGestureRule = `none` | `close` | `hide`;
ts
interface NavigationFailure {
    type: NavigationFailureType;
    msg: string;
    from: RouteRecordRow;
    to?: RouteRecordRow | ToLocationRawRule;
}
ts
type NavigationFailureType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
  • 0 表示 在导航守卫中返回了 false 终止了导航
  • 1 表示 在导航守卫中返回 unknownType 不能识别的结果
  • 2 表示 正在导航中,禁止重复导航
  • 3 表示 在获取页面栈的时候,页面栈不够 level 获取
  • 4 表示 本次导航的 name 值在路由表中未找到
  • 5 表示 本次导航的 path 值在路由表中未找到
  • 6 表示 本次导航的 namepath 在路由表中未找到
  • 7 表示 app及小程序端缓存的顶级页面 currentRoute 不存在 可能是插件的问题
  • 8 表示 本次导航到了相同的路径下,你可能已经在本路径中
  • 9 表示 h5端动态组件加载失败,可能是网络问题,或者是服务器问题
ts
interface NavigationGuardWithThis {
    (to: RouteRecordRow, from: RouteRecordRow): void;
}
ts
type NavTypeRule = 'push' | 'pushTab' | 'replace' | 'replaceAll' | 'back';

ProxyAnimationRule

ts
interface ProxyAnimationRule {
    animationNode: [string, string | undefined] | undefined;
    animationTime: number;
}

PushWithRedirectRule

ts
type PushWithRedirectRule = void | NavigationFailure | undefined;

RouteLocationBaseFormatRaw

ts
interface RouteLocationBaseFormatRaw {
    success: Function;
    fail: Function;
    complete: Function;
    navType: NavTypeRule;
    animationType: string;
    animationDuration: number;
}

RouteLocationNameRaw

ts
interface RouteLocationNameRaw extends RouteLocationObjectRaw {
    name: string;
    params?: Record<string, any>;
    query?: Record<string, any>;
    path?: string;
}

RouteLocationObjectFormatRaw

ts
interface RouteLocationObjectFormatRaw extends RouteLocationBaseFormatRaw {
    path?: string;
    name?: string;
    params?: Record<string, any>;
    query: Record<string, any>;
}

RouteLocationObjectRaw

ts
interface RouteLocationObjectRaw {
    success?: Function;
    fail?: Function;
    complete?: Function;
    navType?: NavTypeRule;
}

RouteLocationPathRaw

ts
interface RouteLocationPathRaw extends RouteLocationObjectRaw {
    query?: Record<string, any>;
    path: string;
}

RouteLocationPushNameRaw

ts
interface RouteLocationPushNameRaw extends RouteLocationNameRaw {
    events?: Record<string, any>;
}

RouteLocationPushPathRaw

ts
interface RouteLocationPushPathRaw extends RouteLocationPathRaw {
    events?: Record<string, any>;
}

RouteLocationPushRaw

ts
interface RouteLocationPushRaw extends RouteLocationObjectRaw {
    animationType?: string;
    animationDuration?: string;
}

RouteLocationRaw

ts
type RouteLocationRaw = string | RouteLocationPathRaw | RouteLocationNameRaw;

RouteRecordBaseRule

ts
interface RouteRecordBaseRule {
    path: string;
    name?: string;
    aliasPath?: string;
    pagesPath?: string;
    redirect?: ((to: RouteRecordRow, from: RouteRecordRow) => RouteLocationRaw) | RouteLocationRaw;
    meta?: RouteMeta;
    beforeEnter?: NavigationGuard;
    props?: Record<string, RoutePropsBaseRule>;
    children?: RouteRecordBaseRule[];
    component?: RouteRecordComponentRule;
    components?: Record<string, RouteRecordComponentRule>;
}

RouteRecordMatcherRow

ts
interface RouteRecordMatcherRow {
    path: RouteRecordBaseRule['path'];
    pagesPath: RouteRecordBaseRule['pagesPath'];
    redirect: RouteRecordBaseRule['redirect'] | undefined;
    name: RouteRecordBaseRule['name'] | undefined;
    meta: Exclude<RouteRecordBaseRule['meta'], void>;
    props: Record<string, RoutePropsBaseRule>;
    beforeEnter: RouteRecordBaseRule['beforeEnter'];
    components: normalizeRouteComponentsRule | null | undefined;
    children: RouteRecordBaseRule[];
    _parentId: number;
    _selfId: number;
    leaveGuards: Set<NavigationGuard>;
    updateGuards: Set<NavigationGuard>;
}

RouteRecordName

ts
type RouteRecordName = string | symbol;

RouteRecordRow

ts
interface RouteRecordRow {
    fullPath: string;
    path: string;
    params: Record<string, any>;
    query: Record<string, any>;
    meta: Exclude<RouteRecordBaseRule['meta'], void>;
    name: RouteRecordBaseRule['name'] | undefined;
    matched: RouteRecordMatched[];
    navType: NavTypeRule;
}

platformRule

ts
type platformRule = 
    `h5` | 
    `app` | 
    `mp-toutiao` | 
    `mp-weixin` |
    `mp-baidu` | 
    `mp-alipay` | 
    `mp-qq` |
    `mp-lark` |
    `mp-jd` |
    `mp-kuaishou`
常用类型约束 has loaded