引入
ts
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter,
getRoutePartUrl,
getUrlInMP
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter,
getRoutePartUrl,
getUrlInMP
} from 't-comm/lib/router/index';
// 只支持 ESM 的项目
import {
findRouteName,
getRouterFuncPath,
getH5CurrentUrl,
uniHookRouter,
getRoutePartUrl,
getUrlInMP
} 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 的参数 |
返回: 解析结果
示例
ts
getRouterFuncPath('/foo/bar');
// { path: '/foo/bar', other: {} }
getRouterFuncPath({ path: '/foo/bar', query: { a: 1 } });
// { path: '/foo/bar', other: { query: { a: 1 } } }
getRouterFuncPath({ name: 'home' });
// { path: undefined, other: {} }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,
})getRoutePartUrl()
描述:获取当前路由部分的 url
参数:
示例
ts
// 在当前页面中调用
getRoutePartUrl();
// 'pages/home/home?from=index'getUrlInMP(baseLink)
描述:小程序中,获取页面对应的 url 链接
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| baseLink | string | 基础 URL |
示例
ts
// 在当前页面中调用
getUrlInMP('https://example.com');
// 'https://example.com/pages/home/home?from=index'