文档 / 工具库 API / route.uts

route 路由

跳转分发、URL 拼参与页面入参取值。

01 / Signature

函数签名

函数签名说明
openPageopenPage(url: string, type: string = 'navigateTo'): void只认 navigateTo / redirectTo / switchTab / reLaunch 四档;空串直接返回,未知 type 静默不跳
buildUrlbuildUrl(path: string, params: UTSJSONObject): string一层键值拼成 path?k=v&k=v;空对象原样返回 path(不带 ?)
getQuerygetQuery(options: OnLoadOptions, key: string, fallback: string = ''): string取 onLoad 入参;缺失、null、空串一律回落 fallback
02 / Example

示例

route
openPage('/pages/detail/detail?id=7')
openPage('/pages/index/index', 'reLaunch')

buildUrl('/pages/detail', { id: 7, from: 'list' } as UTSJSONObject)
// '/pages/detail?id=7&from=list'

buildUrl('/pages/detail', { keyword: '中文 &=值' } as UTSJSONObject)
// '/pages/detail?keyword=%E4%B8%AD%E6%96%87%20%26%3D%E5%80%BC'

onLoad((options) => {
	const id = getQuery(options, 'id', '0')
})
03 / Notes

注意事项

四分支显式写死,不做 uni[type] 动态派发(UTS 不支持);跳转失败打一条 [colorui-x] <type> 失败: <url> 告警,同消息只打一次。只做跳转,不校验 url 前缀。getQuery 不解码;buildUrl 只做一层编码,嵌套对象会变成 [object Object]。