引入
ts
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter
} from 't-comm/lib/router/index';
// 只支持 ESM 的项目
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter
} from 't-comm/es/router/index';
findRouteName(path, routes)
描述:
根据路由表,找到 path 对应的 路由名称
参数:
参数名 | 类型 | 描述 |
---|---|---|
path | string | 路由路径 |
routes | array | 路由表 |
返回: object
匹配到的路由信息
示例
ts
const { name, params, meta, path } = findRouteName(rawPath, ALL_ROUTES) || {};
console.log('name', name);
getRouterFuncPath(route)
描述:
根据路由跳转时的参数,提取 path 和其他参数
参数:
参数名 | 描述 |
---|---|
route | $router.push 或者 $router.replace 的参数 |
返回:
解析结果
getH5CurrentUrl(route)
描述:
小程序下,获取对应的 H5 路由信息
参数:
参数名 | 类型 | 描述 |
---|---|---|
route | Object | 路由信息 |
返回:
H5 Url
示例
ts
getH5CurrentUrl(this.$route);
uniHookRouter()
描述:
拦截路由
参数:
示例
ts
uniHookRouter({
navigateToHooks: [
() => console.log('1')
],
navigateBackHooks: [
() => console.log('2')
],
redirectToHooks: [
() => console.log('3')
],
debug: true,
})