1# @ohos.router (页面路由)(不推荐) 2 3本模块提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、同应用内的某个页面替换当前页面、返回上一页面或指定的页面等。 4 5推荐使用[Navigation组件](../../ui/arkts-navigation-navigation.md)作为应用路由框架。 6 7> **说明** 8> 9> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> 11> - 页面路由需要在页面渲染完成之后才能调用,在onInit和onReady生命周期中页面还处于渲染阶段,禁止调用页面路由方法。 12> 13> - 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](./js-apis-arkui-UIContext.md#uicontext)说明。 14> 15> - 从API version 10开始,可以通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getRouter](./js-apis-arkui-UIContext.md#getrouter)方法获取当前UI上下文关联的[Router](./js-apis-arkui-UIContext.md#router)对象。 16> 17> - 如果使用传入callback形式的pushUrl,pushNamedRoute接口,则在callback中使用getLength等接口获取的栈信息是中间态的栈信息,可能和最终状态不一致。 18 19## 导入模块 20 21``` 22import { router } from '@kit.ArkUI'; 23``` 24 25## router.pushUrl<sup>9+</sup> 26 27pushUrl(options: RouterOptions): Promise<void> 28 29跳转到应用内的指定页面。 30 31**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 32 33**系统能力:** SystemCapability.ArkUI.ArkUI.Full 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| ------- | ------------------------------- | ---- | --------- | 39| options | [RouterOptions](#routeroptions) | 是 | 跳转页面描述信息。 | 40 41**返回值:** 42 43| 类型 | 说明 | 44| ------------------- | --------- | 45| Promise<void> | 异常返回结果。 | 46 47**错误码:** 48 49以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 50 51| 错误码ID | 错误信息 | 52| --------- | ------- | 53| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 54| 100001 | Internal error. | 55| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 56| 100003 | Page stack error. Too many pages are pushed. | 57 58**示例:** 59 60```ts 61import { BusinessError } from '@kit.BasicServicesKit'; 62 63class innerParams { 64 data3:number[] 65 66 constructor(tuple:number[]) { 67 this.data3 = tuple 68 } 69} 70 71class routerParams { 72 data1:string 73 data2:innerParams 74 75 constructor(str:string, tuple:number[]) { 76 this.data1 = str 77 this.data2 = new innerParams(tuple) 78 } 79} 80 81try { 82 router.pushUrl({ 83 url: 'pages/routerpage2', 84 params: new routerParams('message' ,[123,456,789]) 85 }) 86} catch (err) { 87 console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 88} 89``` 90 91## router.pushUrl<sup>9+</sup> 92 93pushUrl(options: RouterOptions, callback: AsyncCallback<void>): void 94 95跳转到应用内的指定页面。 96 97**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 98 99**系统能力:** SystemCapability.ArkUI.ArkUI.Full 100 101**参数:** 102 103| 参数名 | 类型 | 必填 | 说明 | 104| ------- | ------------------------------- | ---- | --------- | 105| options | [RouterOptions](#routeroptions) | 是 | 跳转页面描述信息。 | 106| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 107 108**错误码:** 109 110以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 111 112| 错误码ID | 错误信息 | 113| --------- | ------- | 114| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 115| 100001 | Internal error. | 116| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 117| 100003 | Page stack error. Too many pages are pushed. | 118 119**示例:** 120 121```ts 122class innerParams { 123 data3:number[] 124 125 constructor(tuple:number[]) { 126 this.data3 = tuple 127 } 128} 129 130class routerParams { 131 data1:string 132 data2:innerParams 133 134 constructor(str:string, tuple:number[]) { 135 this.data1 = str 136 this.data2 = new innerParams(tuple) 137 } 138} 139 140router.pushUrl({ 141 url: 'pages/routerpage2', 142 params: new routerParams('message' ,[123,456,789]) 143}, (err) => { 144 if (err) { 145 console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 146 return; 147 } 148 console.info('pushUrl success'); 149}) 150``` 151## router.pushUrl<sup>9+</sup> 152 153pushUrl(options: RouterOptions, mode: RouterMode): Promise<void> 154 155跳转到应用内的指定页面。 156 157**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 158 159**系统能力:** SystemCapability.ArkUI.ArkUI.Full 160 161**参数:** 162 163| 参数名 | 类型 | 必填 | 说明 | 164| ------- | ------------------------------- | ---- | ---------- | 165| options | [RouterOptions](#routeroptions) | 是 | 跳转页面描述信息。 | 166| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 167 168**返回值:** 169 170| 类型 | 说明 | 171| ------------------- | --------- | 172| Promise<void> | 异常返回结果。 | 173 174**错误码:** 175 176以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 177 178| 错误码ID | 错误信息 | 179| --------- | ------- | 180| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 181| 100001 | Internal error. | 182| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 183| 100003 | Page stack error. Too many pages are pushed. | 184 185**示例:** 186 187```ts 188import { BusinessError } from '@kit.BasicServicesKit'; 189 190class innerParams { 191 data3:number[] 192 193 constructor(tuple:number[]) { 194 this.data3 = tuple 195 } 196} 197 198class routerParams { 199 data1:string 200 data2:innerParams 201 202 constructor(str:string, tuple:number[]) { 203 this.data1 = str 204 this.data2 = new innerParams(tuple) 205 } 206} 207 208try { 209 router.pushUrl({ 210 url: 'pages/routerpage2', 211 params: new routerParams('message' ,[123,456,789]) 212 }, router.RouterMode.Standard) 213} catch (err) { 214 console.error(`pushUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 215} 216``` 217 218## router.pushUrl<sup>9+</sup> 219 220pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 221 222跳转到应用内的指定页面。 223 224**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 225 226**系统能力:** SystemCapability.ArkUI.ArkUI.Full 227 228**参数:** 229 230| 参数名 | 类型 | 必填 | 说明 | 231| ------- | ------------------------------- | ---- | ---------- | 232| options | [RouterOptions](#routeroptions) | 是 | 跳转页面描述信息。 | 233| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 234| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 235 236**错误码:** 237 238以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 239 240| 错误码ID | 错误信息 | 241| --------- | ------- | 242| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 243| 100001 | Internal error. | 244| 100002 | Uri error. The URI of the page to redirect is incorrect or does not exist. | 245| 100003 | Page stack error. Too many pages are pushed. | 246 247**示例:** 248 249```ts 250class innerParams { 251 data3:number[] 252 253 constructor(tuple:number[]) { 254 this.data3 = tuple 255 } 256} 257 258class routerParams { 259 data1:string 260 data2:innerParams 261 262 constructor(str:string, tuple:number[]) { 263 this.data1 = str 264 this.data2 = new innerParams(tuple) 265 } 266} 267 268router.pushUrl({ 269 url: 'pages/routerpage2', 270 params: new routerParams('message' ,[123,456,789]) 271}, router.RouterMode.Standard, (err) => { 272 if (err) { 273 console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`); 274 return; 275 } 276 console.info('pushUrl success'); 277}) 278``` 279 280## router.replaceUrl<sup>9+</sup> 281 282replaceUrl(options: RouterOptions): Promise<void> 283 284用应用内的某个页面替换当前页面,并销毁被替换的页面。不支持设置页面转场动效,如需设置,推荐使用[Navigation组件](../../ui/arkts-navigation-navigation.md)。 285 286**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 287 288**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 289 290**参数:** 291 292| 参数名 | 类型 | 必填 | 说明 | 293| ------- | ------------------------------- | ---- | ------------------ | 294| options | [RouterOptions](#routeroptions) | 是 | 替换页面描述信息。 | 295 296**返回值:** 297 298| 类型 | 说明 | 299| ------------------- | --------- | 300| Promise<void> | 异常返回结果。 | 301 302**错误码:** 303 304以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 305 306| 错误码ID | 错误信息 | 307| --------- | ------- | 308| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 309| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 310| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 311 312**示例:** 313 314```ts 315import { BusinessError } from '@kit.BasicServicesKit'; 316 317class routerParams { 318 data1:string 319 320 constructor(str:string) { 321 this.data1 = str 322 } 323} 324 325try { 326 router.replaceUrl({ 327 url: 'pages/detail', 328 params: new routerParams('message') 329 }) 330} catch (err) { 331 console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 332} 333``` 334 335## router.replaceUrl<sup>9+</sup> 336 337replaceUrl(options: RouterOptions, callback: AsyncCallback<void>): void 338 339用应用内的某个页面替换当前页面,并销毁被替换的页面。 340 341**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 342 343**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 344 345**参数:** 346 347| 参数名 | 类型 | 必填 | 说明 | 348| ------- | ------------------------------- | ---- | ------------------ | 349| options | [RouterOptions](#routeroptions) | 是 | 替换页面描述信息。 | 350| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 351 352**错误码:** 353 354以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 355 356| 错误码ID | 错误信息 | 357| --------- | ------- | 358| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 359| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 360| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 361 362**示例:** 363 364```ts 365class routerParams { 366 data1:string 367 368 constructor(str:string) { 369 this.data1 = str 370 } 371} 372 373router.replaceUrl({ 374 url: 'pages/detail', 375 params: new routerParams('message') 376}, (err) => { 377 if (err) { 378 console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`); 379 return; 380 } 381 console.info('replaceUrl success'); 382}) 383``` 384 385## router.replaceUrl<sup>9+</sup> 386 387replaceUrl(options: RouterOptions, mode: RouterMode): Promise<void> 388 389用应用内的某个页面替换当前页面,并销毁被替换的页面。 390 391**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 392 393**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| ------- | ------------------------------- | ---- | ---------- | 399| options | [RouterOptions](#routeroptions) | 是 | 替换页面描述信息。 | 400| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 401 402 403**返回值:** 404 405| 类型 | 说明 | 406| ------------------- | --------- | 407| Promise<void> | 异常返回结果。 | 408 409**错误码:** 410 411以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 412 413| 错误码ID | 错误信息 | 414| --------- | ------- | 415| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 416| 100001 | Failed to get the delegate. This error code is thrown only in the standard system. | 417| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 418 419**示例:** 420 421```ts 422import { BusinessError } from '@kit.BasicServicesKit'; 423 424class routerParams { 425 data1:string 426 427 constructor(str:string) { 428 this.data1 = str 429 } 430} 431 432try { 433 router.replaceUrl({ 434 url: 'pages/detail', 435 params: new routerParams('message') 436 }, router.RouterMode.Standard) 437} catch (err) { 438 console.error(`replaceUrl failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 439} 440``` 441 442## router.replaceUrl<sup>9+</sup> 443 444replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 445 446用应用内的某个页面替换当前页面,并销毁被替换的页面。 447 448**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 449 450**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 451 452**参数:** 453 454| 参数名 | 类型 | 必填 | 说明 | 455| ------- | ------------------------------- | ---- | ---------- | 456| options | [RouterOptions](#routeroptions) | 是 | 替换页面描述信息。 | 457| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 458| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 459 460**错误码:** 461 462以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 463 464| 错误码ID | 错误信息 | 465| --------- | ------- | 466| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 467| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 468| 200002 | Uri error. The URI of the page to be used for replacement is incorrect or does not exist. | 469 470**示例:** 471 472```ts 473class routerParams { 474 data1:string 475 476 constructor(str:string) { 477 this.data1 = str 478 } 479} 480 481router.replaceUrl({ 482 url: 'pages/detail', 483 params: new routerParams('message') 484}, router.RouterMode.Standard, (err) => { 485 if (err) { 486 console.error(`replaceUrl failed, code is ${err.code}, message is ${err.message}`); 487 return; 488 } 489 console.info('replaceUrl success'); 490}); 491 492``` 493 494## router.pushNamedRoute<sup>10+</sup> 495 496pushNamedRoute(options: NamedRouterOptions): Promise<void> 497 498跳转到指定的命名路由页面。 499 500**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 501 502**系统能力:** SystemCapability.ArkUI.ArkUI.Full 503 504**参数:** 505 506| 参数名 | 类型 | 必填 | 说明 | 507| ------- | ------------------------------- | ---- | --------- | 508| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 509 510**返回值:** 511 512| 类型 | 说明 | 513| ------------------- | --------- | 514| Promise<void> | 异常返回结果。 | 515 516**错误码:** 517 518以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 519 520| 错误码ID | 错误信息 | 521| --------- | ------- | 522| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 523| 100001 | Internal error. | 524| 100003 | Page stack error. Too many pages are pushed. | 525| 100004 | Named route error. The named route does not exist. | 526 527**示例:** 528 529```ts 530import { BusinessError } from '@kit.BasicServicesKit'; 531 532class innerParams { 533 data3:number[] 534 535 constructor(tuple:number[]) { 536 this.data3 = tuple 537 } 538} 539 540class routerParams { 541 data1:string 542 data2:innerParams 543 544 constructor(str:string, tuple:number[]) { 545 this.data1 = str 546 this.data2 = new innerParams(tuple) 547 } 548} 549 550try { 551 router.pushNamedRoute({ 552 name: 'myPage', 553 params: new routerParams('message' ,[123,456,789]) 554 }) 555} catch (err) { 556 console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 557} 558``` 559 560详细示例请参考:[UI开发-页面路由](../../ui/arkts-routing.md#命名路由) 561 562## router.pushNamedRoute<sup>10+</sup> 563 564pushNamedRoute(options: NamedRouterOptions, callback: AsyncCallback<void>): void 565 566跳转到指定的命名路由页面。 567 568**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 569 570**系统能力:** SystemCapability.ArkUI.ArkUI.Full 571 572**参数:** 573 574| 参数名 | 类型 | 必填 | 说明 | 575| ------- | ------------------------------- | ---- | --------- | 576| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 577| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 578 579**错误码:** 580 581以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 582 583| 错误码ID | 错误信息 | 584| --------- | ------- | 585| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 586| 100001 | Internal error. | 587| 100003 | Page stack error. Too many pages are pushed. | 588| 100004 | Named route error. The named route does not exist. | 589 590**示例:** 591 592```ts 593class innerParams { 594 data3:number[] 595 596 constructor(tuple:number[]) { 597 this.data3 = tuple 598 } 599} 600 601class routerParams { 602 data1:string 603 data2:innerParams 604 605 constructor(str:string, tuple:number[]) { 606 this.data1 = str 607 this.data2 = new innerParams(tuple) 608 } 609} 610 611router.pushNamedRoute({ 612 name: 'myPage', 613 params: new routerParams('message' ,[123,456,789]) 614}, (err) => { 615 if (err) { 616 console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`); 617 return; 618 } 619 console.info('pushNamedRoute success'); 620}) 621``` 622## router.pushNamedRoute<sup>10+</sup> 623 624pushNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise<void> 625 626跳转到指定的命名路由页面。 627 628**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 629 630**系统能力:** SystemCapability.ArkUI.ArkUI.Full 631 632**参数:** 633 634| 参数名 | 类型 | 必填 | 说明 | 635| ------- | ------------------------------- | ---- | ---------- | 636| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 637| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 638 639**返回值:** 640 641| 类型 | 说明 | 642| ------------------- | --------- | 643| Promise<void> | 异常返回结果。 | 644 645**错误码:** 646 647以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 648 649| 错误码ID | 错误信息 | 650| --------- | ------- | 651| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 652| 100001 | Internal error. | 653| 100003 | Page stack error. Too many pages are pushed. | 654| 100004 | Named route error. The named route does not exist. | 655 656**示例:** 657 658```ts 659import { BusinessError } from '@kit.BasicServicesKit'; 660 661class innerParams { 662 data3:number[] 663 664 constructor(tuple:number[]) { 665 this.data3 = tuple 666 } 667} 668 669class routerParams { 670 data1:string 671 data2:innerParams 672 673 constructor(str:string, tuple:number[]) { 674 this.data1 = str 675 this.data2 = new innerParams(tuple) 676 } 677} 678 679try { 680 router.pushNamedRoute({ 681 name: 'myPage', 682 params: new routerParams('message' ,[123,456,789]) 683 }, router.RouterMode.Standard) 684} catch (err) { 685 console.error(`pushNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 686} 687``` 688 689## router.pushNamedRoute<sup>10+</sup> 690 691pushNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 692 693跳转到指定的命名路由页面。 694 695**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 696 697**系统能力:** SystemCapability.ArkUI.ArkUI.Full 698 699**参数:** 700 701| 参数名 | 类型 | 必填 | 说明 | 702| ------- | ------------------------------- | ---- | ---------- | 703| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 跳转页面描述信息。 | 704| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 705| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 710 711| 错误码ID | 错误信息 | 712| --------- | ------- | 713| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 714| 100001 | Internal error. | 715| 100003 | Page stack error. Too many pages are pushed. | 716| 100004 | Named route error. The named route does not exist. | 717 718**示例:** 719 720```ts 721class innerParams { 722 data3:number[] 723 724 constructor(tuple:number[]) { 725 this.data3 = tuple 726 } 727} 728 729class routerParams { 730 data1:string 731 data2:innerParams 732 733 constructor(str:string, tuple:number[]) { 734 this.data1 = str 735 this.data2 = new innerParams(tuple) 736 } 737} 738 739router.pushNamedRoute({ 740 name: 'myPage', 741 params: new routerParams('message' ,[123,456,789]) 742}, router.RouterMode.Standard, (err) => { 743 if (err) { 744 console.error(`pushNamedRoute failed, code is ${err.code}, message is ${err.message}`); 745 return; 746 } 747 console.info('pushNamedRoute success'); 748}) 749``` 750 751## router.replaceNamedRoute<sup>10+</sup> 752 753replaceNamedRoute(options: NamedRouterOptions): Promise<void> 754 755用指定的命名路由页面替换当前页面,并销毁被替换的页面。 756 757**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 758 759**系统能力:** SystemCapability.ArkUI.ArkUI.Full 760 761**参数:** 762 763| 参数名 | 类型 | 必填 | 说明 | 764| ------- | ------------------------------- | ---- | ------------------ | 765| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 替换页面描述信息。 | 766 767**返回值:** 768 769| 类型 | 说明 | 770| ------------------- | --------- | 771| Promise<void> | 异常返回结果。 | 772 773**错误码:** 774 775以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 776 777| 错误码ID | 错误信息 | 778| --------- | ------- | 779| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 780| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 781| 100004 | Named route error. The named route does not exist. | 782 783**示例:** 784 785```ts 786import { BusinessError } from '@kit.BasicServicesKit'; 787 788class routerParams { 789 data1:string 790 791 constructor(str:string) { 792 this.data1 = str 793 } 794} 795 796try { 797 router.replaceNamedRoute({ 798 name: 'myPage', 799 params: new routerParams('message') 800 }) 801} catch (err) { 802 console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 803} 804``` 805 806## router.replaceNamedRoute<sup>10+</sup> 807 808replaceNamedRoute(options: NamedRouterOptions, callback: AsyncCallback<void>): void 809 810用指定的命名路由页面替换当前页面,并销毁被替换的页面。 811 812**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 813 814**系统能力:** SystemCapability.ArkUI.ArkUI.Full 815 816**参数:** 817 818| 参数名 | 类型 | 必填 | 说明 | 819| ------- | ------------------------------- | ---- | ------------------ | 820| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 替换页面描述信息。 | 821| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 822 823**错误码:** 824 825以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 826 827| 错误码ID | 错误信息 | 828| --------- | ------- | 829| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 830| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 831| 100004 | Named route error. The named route does not exist. | 832 833**示例:** 834 835```ts 836class routerParams { 837 data1:string 838 839 constructor(str:string) { 840 this.data1 = str 841 } 842} 843 844router.replaceNamedRoute({ 845 name: 'myPage', 846 params: new routerParams('message') 847}, (err) => { 848 if (err) { 849 console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`); 850 return; 851 } 852 console.info('replaceNamedRoute success'); 853}) 854``` 855 856## router.replaceNamedRoute<sup>10+</sup> 857 858replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode): Promise<void> 859 860用指定的命名路由页面替换当前页面,并销毁被替换的页面。 861 862**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 863 864**系统能力:** SystemCapability.ArkUI.ArkUI.Full 865 866**参数:** 867 868| 参数名 | 类型 | 必填 | 说明 | 869| ------- | ------------------------------- | ---- | ---------- | 870| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 替换页面描述信息。 | 871| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 872 873 874**返回值:** 875 876| 类型 | 说明 | 877| ------------------- | --------- | 878| Promise<void> | 异常返回结果。 | 879 880**错误码:** 881 882以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 883 884| 错误码ID | 错误信息 | 885| --------- | ------- | 886| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 887| 100001 | Failed to get the delegate. This error code is thrown only in the standard system. | 888| 100004 | Named route error. The named route does not exist. | 889 890**示例:** 891 892```ts 893import { BusinessError } from '@kit.BasicServicesKit'; 894 895class routerParams { 896 data1:string 897 898 constructor(str:string) { 899 this.data1 = str 900 } 901} 902 903try { 904 router.replaceNamedRoute({ 905 name: 'myPage', 906 params: new routerParams('message') 907 }, router.RouterMode.Standard) 908} catch (err) { 909 console.error(`replaceNamedRoute failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 910} 911``` 912 913## router.replaceNamedRoute<sup>10+</sup> 914 915replaceNamedRoute(options: NamedRouterOptions, mode: RouterMode, callback: AsyncCallback<void>): void 916 917用指定的命名路由页面替换当前页面,并销毁被替换的页面。 918 919**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 920 921**系统能力:** SystemCapability.ArkUI.ArkUI.Full 922 923**参数:** 924 925| 参数名 | 类型 | 必填 | 说明 | 926| ------- | ------------------------------- | ---- | ---------- | 927| options | [NamedRouterOptions](#namedrouteroptions10) | 是 | 替换页面描述信息。 | 928| mode | [RouterMode](#routermode9) | 是 | 跳转页面使用的模式。 | 929| callback | AsyncCallback<void> | 是 | 异常响应回调。 | 930 931**错误码:** 932 933以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 934 935| 错误码ID | 错误信息 | 936| --------- | ------- | 937| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 938| 100001 | The UI execution context is not found. This error code is thrown only in the standard system. | 939| 100004 | Named route error. The named route does not exist. | 940 941**示例:** 942 943```ts 944class routerParams { 945 data1:string 946 947 constructor(str:string) { 948 this.data1 = str 949 } 950} 951 952router.replaceNamedRoute({ 953 name: 'myPage', 954 params: new routerParams('message') 955}, router.RouterMode.Standard, (err) => { 956 if (err) { 957 console.error(`replaceNamedRoute failed, code is ${err.code}, message is ${err.message}`); 958 return; 959 } 960 console.info('replaceNamedRoute success'); 961}); 962 963``` 964 965## router.back 966 967back(options?: RouterOptions ): void 968 969返回上一页面或指定的页面,会删除当前页面与指定页面之间的所有页面。 970 971**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 972 973**系统能力:** SystemCapability.ArkUI.ArkUI.Full 974 975**参数:** 976 977| 参数名 | 类型 | 必填 | 说明 | 978| ------- | ------------------------------- | ---- | ------------------------------------------------------------ | 979| options | [RouterOptions](#routeroptions) | 否 | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。back是返回接口,url设置为特殊值"/"不生效。 | 980 981**示例:** 982 983```ts 984router.back({url:'pages/detail'}); 985``` 986 987## router.back<sup>12+</sup> 988 989back(index: number, params?: Object): void; 990 991返回指定的页面,会删除当前页面与指定页面之间的所有页面。 992 993**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 994 995**系统能力:** SystemCapability.ArkUI.ArkUI.Full 996 997**参数:** 998 999| 参数名 | 类型 | 必填 | 说明 | 1000| ------- | ------------------------------- | ---- | ---------- | 1001| index | number | 是 | 跳转目标页面的索引值。 | 1002| params | Object | 否 | 页面返回时携带的参数。 | 1003 1004**示例:** 1005 1006```ts 1007router.back(1); 1008``` 1009```ts 1010router.back(1, {info: '来自Home页'}); //携带参数返回 1011``` 1012 1013## router.clear 1014 1015clear(): void 1016 1017清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。 1018 1019**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1020 1021**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1022 1023**示例:** 1024 1025```ts 1026router.clear(); 1027``` 1028 1029## router.getLength 1030 1031getLength(): string 1032 1033获取当前在页面栈内的页面数量。 1034 1035**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1036 1037**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1038 1039**返回值:** 1040 1041| 类型 | 说明 | 1042| ------ | ------------------ | 1043| string | 页面数量,页面栈支持最大数值是32。 | 1044 1045**示例:** 1046 1047```ts 1048let size = router.getLength(); 1049console.log('pages stack size = ' + size); 1050``` 1051 1052## router.getState 1053 1054getState(): RouterState 1055 1056获取栈顶页面的状态信息。 1057 1058**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1059 1060**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1061 1062**返回值:** 1063 1064| 类型 | 说明 | 1065| --------------------------- | ------- | 1066| [RouterState](#routerstate) | 页面状态信息。 | 1067 1068**示例:** 1069 1070```ts 1071let page = router.getState(); 1072console.log('current index = ' + page.index); 1073console.log('current name = ' + page.name); 1074console.log('current path = ' + page.path); 1075``` 1076 1077## router.getStateByIndex<sup>12+</sup> 1078 1079getStateByIndex(index: number): RouterState | undefined 1080 1081通过索引值获取对应页面的状态信息。 1082 1083**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1084 1085**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1086 1087**参数:** 1088 1089| 参数名 | 类型 | 必填 | 说明 | 1090| ------- | ------------------------------- | ---- | ---------- | 1091| index | number | 是 | 表示要获取的页面索引。 | 1092 1093**返回值:** 1094 1095| 类型 | 说明 | 1096| --------------------------- | ------- | 1097| [RouterState](#routerstate) \| undefined | 返回页面状态信息。索引不存在时返回undefined。 | 1098 1099**示例:** 1100 1101```ts 1102let options:router.RouterState | undefined = router.getStateByIndex(1); 1103if (options != undefined) { 1104 console.log('index = ' + options.index); 1105 console.log('name = ' + options.name); 1106 console.log('path = ' + options.path); 1107 console.log('params = ' + options.params); 1108} 1109``` 1110## router.getStateByUrl<sup>12+</sup> 1111 1112getStateByUrl(url: string): Array<RouterState> 1113 1114通过url获取对应页面的状态信息。 1115 1116**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1117 1118**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1119 1120**参数:** 1121 1122| 参数名 | 类型 | 必填 | 说明 | 1123| ------- | ------------------------------- | ---- | ---------- | 1124| url | string | 是 | 表示要获取对应页面信息的url。 | 1125 1126**返回值:** 1127 1128| 类型 | 说明 | 1129| --------------------------- | ------- | 1130| Array<[RouterState](#routerstate)> | 页面状态信息。 | 1131 1132**示例:** 1133 1134```ts 1135let options:Array<router.RouterState> = router.getStateByUrl('pages/index'); 1136for (let i: number = 0; i < options.length; i++) { 1137 console.log('index = ' + options[i].index); 1138 console.log('name = ' + options[i].name); 1139 console.log('path = ' + options[i].path); 1140 console.log('params = ' + options[i].params); 1141} 1142``` 1143 1144## RouterState 1145 1146页面状态信息。 1147 1148**系统能力:** SystemCapability.ArkUI.ArkUI.Full。 1149 1150| 名称 | 类型 | 必填 | 说明 | 1151| ----- | ------ | ---- | ------------------------------------------------------------ | 1152| index | number | 是 | 表示当前页面在页面栈中的索引。从栈底到栈顶,index从1开始递增。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1153| name | string | 是 | 表示当前页面的名称,即对应文件名。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1154| path | string | 是 | 表示当前页面的路径。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1155| params<sup>12+</sup> | Object | 是 | 表示当前页面携带的参数。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1156 1157## router.showAlertBeforeBackPage<sup>9+</sup> 1158 1159showAlertBeforeBackPage(options: EnableAlertOptions): void 1160 1161开启页面返回询问对话框。 1162 1163**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1164 1165**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1166 1167**参数:** 1168 1169| 参数名 | 类型 | 必填 | 说明 | 1170| ------- | ---------------------------------------- | ---- | --------- | 1171| options | [EnableAlertOptions](#enablealertoptions) | 是 | 文本弹窗信息描述。 | 1172 1173**错误码:** 1174 1175以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.router(页面路由)](errorcode-router.md)错误码。 1176 1177| 错误码ID | 错误信息 | 1178| --------- | ------- | 1179| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. | 1180| 100001 | Internal error. | 1181 1182**示例:** 1183 1184```ts 1185import { BusinessError } from '@kit.BasicServicesKit'; 1186 1187try { 1188 router.showAlertBeforeBackPage({ 1189 message: 'Message Info' 1190 }); 1191} catch(err) { 1192 console.error(`showAlertBeforeBackPage failed, code is ${(err as BusinessError).code}, message is ${(err as BusinessError).message}`); 1193} 1194``` 1195## EnableAlertOptions 1196 1197页面返回询问对话框选项。 1198 1199**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1200 1201**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full。 1202 1203| 名称 | 类型 | 必填 | 说明 | 1204| ------- | ------ | ---- | -------- | 1205| message | string | 是 | 询问对话框内容。 | 1206 1207## router.hideAlertBeforeBackPage<sup>9+</sup> 1208 1209hideAlertBeforeBackPage(): void 1210 1211禁用页面返回询问对话框。 1212 1213**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1214 1215**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1216 1217**示例:** 1218 1219```ts 1220router.hideAlertBeforeBackPage(); 1221``` 1222 1223## router.getParams 1224 1225getParams(): Object 1226 1227获取发起跳转的页面往当前页传入的参数。 1228 1229**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1230 1231**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1232 1233**返回值:** 1234 1235| 类型 | 说明 | 1236| ------ | ---------------------------------- | 1237| object | 发起跳转的页面往当前页传入的参数。 | 1238 1239**示例:** 1240 1241```ts 1242router.getParams(); 1243``` 1244 1245## RouterOptions 1246 1247路由跳转选项。 1248 1249**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1250 1251**系统能力:** SystemCapability.ArkUI.ArkUI.Lite。 1252 1253| 名称 | 类型 | 必填 | 说明 | 1254| ------ | ------ | ---- | ------------------------------------------------------------ | 1255| url | string | 是 | 表示目标页面的url,可以用以下两种格式:<br/>- 页面绝对路径,由配置文件中pages列表提供,例如:<br/> - pages/index/index<br/> - pages/detail/detail<br/>- 特殊值,如果url的值是"/",则跳转到首页,首页默认为页面跳转配置项src数组的第一个数据项。 | 1256| params | Object | 否 | 表示路由跳转时要同时传递到目标页面的数据,切换到其他页面时,当前接收的数据失效。跳转到目标页面后,使用router.getParams()获取传递的参数,此外,在类web范式中,参数也可以在页面中直接使用,如this.keyValue(keyValue为跳转时params参数中的key值),如果目标页面中已有该字段,则其值会被传入的字段值覆盖。<br/>**说明:** <br/>params参数不能传递方法和系统接口返回的对象(例如,媒体接口定义和返回的PixelMap对象)。建议开发者提取系统接口返回的对象中需要被传递的基础类型属性,自行构造object类型对象进行传递。 | 1257 1258 1259 > **说明:** 1260 > 页面路由栈支持的最大Page数量为32。 1261 1262## RouterMode<sup>9+</sup> 1263 1264路由跳转模式。 1265 1266**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1267 1268**系统能力:** SystemCapability.ArkUI.ArkUI.Full。 1269 1270| 名称 | 说明 | 1271| -------- | ------------------------------------------------------------ | 1272| Standard | 多实例模式,也是默认情况下的跳转模式。 <br/>目标页面会被添加到页面栈顶,无论栈中是否存在相同url的页面。<br/>**说明:** <br/>不使用路由跳转模式时,则按照默认的多实例模式进行跳转。 | 1273| Single | 单实例模式。<br/>如果目标页面的url已经存在于页面栈中,则该url页面移动到栈顶。<br />如果目标页面的url在页面栈中不存在同url页面,则按照默认的多实例模式进行跳转。 | 1274 1275## NamedRouterOptions<sup>10+</sup> 1276 1277命名路由跳转选项。 1278 1279**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1280 1281**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1282 1283| 名称 | 类型 | 必填 | 说明 | 1284| ------ | ------ | ---- | ------------------------------------------------------------ | 1285| name | string | 是 | 表示目标命名路由页面的name。 | 1286| params | Object | 否 | 表示路由跳转时要同时传递到目标页面的数据。跳转到目标页面后,使用router.getParams()获取传递的参数,此外,在类web范式中,参数也可以在页面中直接使用,如this.keyValue(keyValue为跳转时params参数中的key值),如果目标页面中已有该字段,则其值会被传入的字段值覆盖。 <br/>**说明:** <br/>params参数不能传递方法和系统接口返回的对象(例如,媒体接口定义和返回的PixelMap对象)。建议开发者提取系统接口返回的对象中需要被传递的基础类型属性,自行构造object类型对象进行传递。 | 1287 1288## 完整示例 1289 1290### 基于JS扩展的类Web开发范式 1291 1292以下代码仅适用于javascript文件,不适用于ArkTS文件 1293 1294<!--code_no_check--> 1295 1296```js 1297// 在当前页面中 1298export default { 1299 pushPage() { 1300 router.pushUrl({ 1301 url: 'pages/detail/detail', 1302 params: { 1303 data1: 'message' 1304 } 1305 }); 1306 } 1307} 1308``` 1309<!--code_no_check--> 1310 1311```js 1312// 在detail页面中 1313export default { 1314 onInit() { 1315 console.info('showData1:' + router.getParams()['data1']); 1316 } 1317} 1318``` 1319 1320### 基于TS扩展的声明式开发范式 1321 1322> **说明:** 1323> 1324> 直接使用router可能导致实例不明确的问题,建议使用[getUIContext](js-apis-arkui-UIContext.md#uicontext)获取UIContext实例,并使用[getRouter](js-apis-arkui-UIContext.md#getrouter)获取绑定实例的router。 1325 1326```ts 1327// 通过router.pushUrl跳转至目标页携带params参数 1328import { router } from '@kit.ArkUI'; 1329import { BusinessError } from '@kit.BasicServicesKit' 1330 1331// 定义传递参数的类 1332class innerParams { 1333 array:number[] 1334 1335 constructor(tuple:number[]) { 1336 this.array = tuple 1337 } 1338} 1339 1340class routerParams { 1341 text:string 1342 data:innerParams 1343 1344 constructor(str:string, tuple:number[]) { 1345 this.text = str 1346 this.data = new innerParams(tuple) 1347 } 1348} 1349 1350@Entry 1351@Component 1352struct Index { 1353 async routePage() { 1354 let options:router.RouterOptions = { 1355 url: 'pages/second', 1356 params: new routerParams('这是第一页的值' ,[12, 45, 78]) 1357 } 1358 try { 1359 // 建议使用this.getUIContext().getRouter().pushUrl() 1360 await router.pushUrl(options) 1361 } catch (err) { 1362 console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`) 1363 } 1364 } 1365 1366 build() { 1367 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1368 Text('这是第一页') 1369 .fontSize(50) 1370 .fontWeight(FontWeight.Bold) 1371 Button() { 1372 Text('next page') 1373 .fontSize(25) 1374 .fontWeight(FontWeight.Bold) 1375 }.type(ButtonType.Capsule) 1376 .margin({ top: 20 }) 1377 .backgroundColor('#ccc') 1378 .onClick(() => { 1379 this.routePage() 1380 }) 1381 } 1382 .width('100%') 1383 .height('100%') 1384 } 1385} 1386``` 1387 1388```ts 1389// 在second页面中接收传递过来的参数 1390import { router } from '@kit.ArkUI'; 1391 1392class innerParams { 1393 array:number[] 1394 1395 constructor(tuple:number[]) { 1396 this.array = tuple 1397 } 1398} 1399 1400class routerParams { 1401 text:string 1402 data:innerParams 1403 1404 constructor(str:string, tuple:number[]) { 1405 this.text = str 1406 this.data = new innerParams(tuple) 1407 } 1408} 1409 1410@Entry 1411@Component 1412struct Second { 1413 private content: string = "这是第二页" 1414 // 建议使用this.getUIContext().getRouter().getParams() 1415 @State text: string = (router.getParams() as routerParams).text 1416 @State data: object = (router.getParams() as routerParams).data 1417 @State secondData: string = '' 1418 1419 build() { 1420 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 1421 Text(`${this.content}`) 1422 .fontSize(50) 1423 .fontWeight(FontWeight.Bold) 1424 Text(this.text) 1425 .fontSize(30) 1426 .onClick(() => { 1427 this.secondData = (this.data['array'][1]).toString() 1428 }) 1429 .margin({ top: 20 }) 1430 Text(`第一页传来的数值:${this.secondData}`) 1431 .fontSize(20) 1432 .margin({ top: 20 }) 1433 .backgroundColor('red') 1434 } 1435 .width('100%') 1436 .height('100%') 1437 } 1438} 1439``` 1440 1441## router.push<sup>(deprecated)</sup> 1442 1443push(options: RouterOptions): void 1444 1445跳转到应用内的指定页面。 1446 1447从API version9开始不再维护,建议使用[pushUrl<sup>9+</sup>](#routerpushurl9) 1448 1449**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1450 1451**参数:** 1452 1453| 参数名 | 类型 | 必填 | 说明 | 1454| ------- | ------------------------------- | ---- | --------- | 1455| options | [RouterOptions](#routeroptions) | 是 | 跳转页面描述信息。 | 1456 1457 1458**示例:** 1459 1460```ts 1461class innerParams { 1462 data3:number[] 1463 1464 constructor(tuple:number[]) { 1465 this.data3 = tuple 1466 } 1467} 1468 1469class routerParams { 1470 data1:string 1471 data2:innerParams 1472 1473 constructor(str:string, tuple:number[]) { 1474 this.data1 = str 1475 this.data2 = new innerParams(tuple) 1476 } 1477} 1478 1479router.push({ 1480 url: 'pages/routerpage2', 1481 params: new routerParams('message' ,[123,456,789]) 1482}); 1483``` 1484 1485## router.replace<sup>(deprecated)</sup> 1486 1487replace(options: RouterOptions): void 1488 1489用应用内的某个页面替换当前页面,并销毁被替换的页面。 1490 1491从API version9开始不再维护,建议使用[replaceUrl<sup>9+</sup>](#routerreplaceurl9) 1492 1493**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 1494 1495**参数:** 1496 1497| 参数名 | 类型 | 必填 | 说明 | 1498| ------- | ------------------------------- | ---- | ------------------ | 1499| options | [RouterOptions](#routeroptions) | 是 | 替换页面描述信息。 | 1500 1501**示例:** 1502 1503```ts 1504class routerParams { 1505 data1:string 1506 1507 constructor(str:string) { 1508 this.data1 = str 1509 } 1510} 1511 1512router.replace({ 1513 url: 'pages/detail', 1514 params: new routerParams('message') 1515}); 1516``` 1517 1518## router.enableAlertBeforeBackPage<sup>(deprecated)</sup> 1519 1520enableAlertBeforeBackPage(options: EnableAlertOptions): void 1521 1522开启页面返回询问对话框。 1523 1524从API version9开始不再维护,建议使用[showAlertBeforeBackPage<sup>9+</sup>](#routershowalertbeforebackpage9) 1525 1526**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1527 1528**参数:** 1529 1530| 参数名 | 类型 | 必填 | 说明 | 1531| ------- | ---------------------------------------- | ---- | --------- | 1532| options | [EnableAlertOptions](#enablealertoptions) | 是 | 文本弹窗信息描述。 | 1533 1534**示例:** 1535 1536```ts 1537router.enableAlertBeforeBackPage({ 1538 message: 'Message Info' 1539}); 1540``` 1541 1542## router.disableAlertBeforeBackPage<sup>(deprecated)</sup> 1543 1544disableAlertBeforeBackPage(): void 1545 1546禁用页面返回询问对话框。 1547 1548从API version9开始不再维护,建议使用[hideAlertBeforeBackPage<sup>9+</sup>](#routerhidealertbeforebackpage9) 1549 1550**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1551 1552**示例:** 1553 1554```ts 1555router.disableAlertBeforeBackPage(); 1556```