往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)
@ohos.router (页面路由)(不推荐)
本模块提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、同应用内的某个页面替换当前页面、返回上一页面或指定的页面等。
说明
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
页面路由需要在页面渲染完成之后才能调用,在onInit和onReady生命周期中页面还处于渲染阶段,禁止调用页面路由方法。
本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见UIContext说明。
从API version 10开始,可以通过使用UIContext中的getRouter方法获取当前UI上下文关联的Router对象。
如果使用传入callback形式的pushUrl或pushNamedRoute接口,callback中通过getLength等接口获取的栈信息为中间态的栈信息,可能与栈操作完全结束后,再通过getLength等接口获取的栈信息不一致。
导入模块
import { router } from '@kit.ArkUI';
router.pushUrl9+
pushUrl(options: RouterOptions): Promise<void>
跳转到应用内的指定页面。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | RouterOptions | 是 | 跳转页面描述信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
100001 | Internal error. |
100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. |
100003 | Page stack error. Too many pages are pushed. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
class innerParams {
data3:number[]
constructor(tuple:number[]) {
this.data3 = tuple
}
}
class routerParams {
data1:string
data2:innerParams
constructor(str:string, tuple:number[]) {
this.data1 = str
this.data2 = new innerParams(tuple)
}
}
try {
router.pushUrl({
url: 'pages/routerpage2',
params: new routerParams('message' ,[123,456,789])
})
} catch (err) {
console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`);
}
router.pushUrl9+
pushUrl(options: RouterOptions, callback: AsyncCallback<void>): void
跳转到应用内的指定页面。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | RouterOptions | 是 | 跳转页面描述信息。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
100001 | Internal error. |
100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. |
100003 | Page stack error. Too many pages are pushed. |
示例:
class innerParams {
data3:number[]
constructor(tuple:number[]) {
this.data3 = tuple
}
}
class routerParams {
data1:string
data2:innerParams
constructor(str:string, tuple:number[]) {
this.data1 = str
this.data2 = new innerParams(tuple)
}
}
router.pushUrl({
url: 'pages/routerpage2',
params: new routerParams('message' ,[123,456,789])
}, (err) => {
if (err) {
console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('pushUrl success');
})
router.pushUrl9+
pushUrl(options: RouterOptions, mode: RouterMode): Promise<void>
跳转到应用内的指定页面。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | RouterOptions | 是 | 跳转页面描述信息。 |
mode | RouterMode | 是 | 跳转页面使用的模式。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
100001 | Internal error. |
100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. |
100003 | Page stack error. Too many pages are pushed. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
class innerParams {
data3:number[]
constructor(tuple:number[]) {
this.data3 = tuple
}
}
class routerParams {
data1:string
data2:innerParams
constructor(str:string, tuple:numb