Skip to content

RouterOptions ​

createRouter() 的初始化选项。

ts
import type { RouterOptions } from '@meng-xi/unix-router'

const options: RouterOptions = {
	routes,
	strict: true,
	plugins: [new ParamsPlugin()],
	guardTimeout: 10000,
	readyTimeout: 0
}

字段 ​

字段类型默认值说明
routesRouteConfig[]—必需。路由配置列表,路径须与 pages.json 注册一致
strictbooleantrue严格模式。true 时未匹配的命名路由抛 RouterError ROUTE_NOT_FOUND;false 时仅警告并按路径处理
guardTimeoutnumber10000守卫超时(毫秒),0 禁用。超时输出警告并中止导航
readyTimeoutnumber0就绪超时(毫秒),0 永不超时
interceptUniApibooleanfalseopt-in。启用后拦截 uni.* 原生导航 API(navigateTo / redirectTo / switchTab / reLaunch / navigateBack),外部直接调用也转交 router.* 走完整守卫链。须配合 InterceptorPlugin。受运行时版本支持:Web 4.0 / 微信 4.41 / Android 3.97 / iOS 4.11 / HarmonyOS 4.61,缺失时自动降级并警告
pluginsRouterPlugin[]—插件列表,按需注册扩展能力。传入实例:[new ParamsPlugin(), new InterceptorPlugin(), new EventsPlugin(), new AnimationPlugin()],见插件系统
paramsPersistentbooleanfalse是否默认将 params 持久化到 storage(写入失败自动回退内存)。须配合 ParamsPlugin
animationNavigationAnimation—全局默认导航动画 { type: AnimationType, duration?: number }(duration 默认 300ms)。App 端透传原生 animationType(官方仅 App 支持),H5 端由插件以 Web Animations API 实现。须配合 AnimationPlugin

插件相关选项须配合对应插件

interceptUniApi / paramsPersistent 分别依赖 InterceptorPlugin / ParamsPlugin,注册了选项但未注册对应插件时选项被忽略并输出警告;animation 未注册 AnimationPlugin 时被静默忽略(无警告)。

注意:微信小程序端 <navigator> 组件跳转与点击 tabBar(底层不触发 uni.switchTab)无法被拦截,此场景需在页面 onShow 兜底守卫。

相关 API ​

Released under the MIT License.