1# Component Navigation (Navigation) (Recommended) 2 3Component navigation, implemented using the [Navigation](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md) component, is instrumental for managing transitions between pages and within components. It allows for the passing of redirection parameters across various components and offers various stack operations to simplify access to and reuse of diverse pages. This documentation delves into the display modes, routing operations, subpage management, cross-package navigation, and the effects of navigation transitions. 4 5The [Navigation](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md) component serves as the root view container for route navigation and is commonly used as the root container for pages decorated with @Entry. It supports three display modes: Stack, Split, and Auto. This component is designed for both intra-module and cross-module routing, leveraging component-level routing to provide a more natural and seamless transition between pages. It also offers multiple title bar styles to enhance the cascading between titles and content. In one-time development for multi-device deployment scenarios, the **Navigation** component can automatically adapt to the window size. When the window is large enough, it automatically displays content in columns. 6 7The **Navigation** component consists of a navigation page and subpages. The navigation page consists of the title bar (with the menu bar), content area, and toolbar, and can be hidden through the [hideNavBar](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#hidenavbar9) attribute. Unlike other pages, the navigation page does not reside in the page stack. Routing is used to manage transitions between the navigation page and its subpages, as well as between subpages themselves. 8 9In API version 9, the **Navigation** component must be used together with the [NavRouter](../reference/apis-arkui/arkui-ts/ts-basic-components-navrouter.md) component for page routing. Since API version 10, whenever possible, use [NavPathStack](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#navpathstack10) instead to implement page routing. 10 11 12## Setting the Page Display Mode 13 14The **Navigation** component uses the **mode** attribute to set the page display mode. 15 16- Adaptive Mode 17 18 By default, the **Navigation** component is in adaptive mode. In this case, the **mode** attribute is **NavigationMode.Auto**. In adaptive mode, when the device width is greater than or equal to the threshold (520 vp for API version 9 and earlier and 600 vp for API version 10 and later), the **Navigation** component uses the column mode. Otherwise, the **Navigation** component uses the single-page mode. 19 20 21 ``` 22 Navigation() { 23 // ... 24 } 25 .mode(NavigationMode.Auto) 26 ``` 27 28- Single-page mode 29 30 **Figure 1** Single-page mode 31 32  33 34 Set **mode** to **NavigationMode.Stack** so that the **Navigation** component is displayed on a single page. 35 36 37 ```ts 38 Navigation() { 39 // ... 40 } 41 .mode(NavigationMode.Stack) 42 ``` 43 44  45 46- Column mode 47 48 **Figure 2** Column mode 49 50  51 52 Set **mode** to **NavigationMode.Split** so that the **Navigation** component is displayed in columns. 53 54 55 ```ts 56 @Entry 57 @Component 58 struct NavigationExample { 59 @State TooTmp: ToolbarItem = { 60 'value': "func", 'icon': "./image/ic_public_highlights.svg", 'action': () => { 61 } 62 } 63 @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack() 64 private arr: number[] = [1, 2, 3]; 65 66 @Builder 67 PageMap(name: string) { 68 if (name === "NavDestinationTitle1") { 69 pageOneTmp() 70 } else if (name === "NavDestinationTitle2") { 71 pageTwoTmp() 72 } else if (name === "NavDestinationTitle3") { 73 pageThreeTmp() 74 } 75 } 76 77 build() { 78 Column() { 79 Navigation(this.pageInfos) { 80 TextInput({ placeholder: 'Search...' }) 81 .width("90%") 82 .height(40) 83 .backgroundColor('#FFFFFF') 84 85 List({ space: 12 }) { 86 ForEach(this.arr, (item: number) => { 87 ListItem() { 88 Text("Page" + item) 89 .width("100%") 90 .height(72) 91 .backgroundColor('#FFFFFF') 92 .borderRadius(24) 93 .fontSize(16) 94 .fontWeight(500) 95 .textAlign(TextAlign.Center) 96 .onClick(() => { 97 this.pageInfos.pushPath({ name: "NavDestinationTitle" + item }) 98 }) 99 } 100 }, (item: number) => item.toString()) 101 } 102 .width("90%") 103 .margin({ top: 12 }) 104 } 105 .title("Main Title") 106 .mode(NavigationMode.Split) 107 .navDestination(this.PageMap) 108 .menus([ 109 { 110 value: "", icon: "./image/ic_public_search.svg", action: () => { 111 } 112 }, 113 { 114 value: "", icon: "./image/ic_public_add.svg", action: () => { 115 } 116 }, 117 { 118 value: "", icon: "./image/ic_public_add.svg", action: () => { 119 } 120 }, 121 { 122 value: "", icon: "./image/ic_public_add.svg", action: () => { 123 } 124 }, 125 { 126 value: "", icon: "./image/ic_public_add.svg", action: () => { 127 } 128 } 129 ]) 130 .toolbarConfiguration([this.TooTmp, this.TooTmp, this.TooTmp]) 131 } 132 .height('100%') 133 .width('100%') 134 .backgroundColor('#F1F3F5') 135 } 136 } 137 138 // PageOne.ets 139 @Component 140 export struct pageOneTmp { 141 @Consume('pageInfos') pageInfos: NavPathStack; 142 143 build() { 144 NavDestination() { 145 Column() { 146 Text("NavDestinationContent1") 147 }.width('100%').height('100%') 148 }.title("NavDestinationTitle1") 149 .onBackPressed(() => { 150 const popDestinationInfo = this.pageInfos.pop() // Pop the top element out of the navigation stack. 151 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 152 return true 153 }) 154 } 155 } 156 157 // PageTwo.ets 158 @Component 159 export struct pageTwoTmp { 160 @Consume('pageInfos') pageInfos: NavPathStack; 161 162 build() { 163 NavDestination() { 164 Column() { 165 Text("NavDestinationContent2") 166 }.width('100%').height('100%') 167 }.title("NavDestinationTitle2") 168 .onBackPressed(() => { 169 const popDestinationInfo = this.pageInfos.pop() // Pop the top element out of the navigation stack. 170 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 171 return true 172 }) 173 } 174 } 175 176 // PageThree.ets 177 @Component 178 export struct pageThreeTmp { 179 @Consume('pageInfos') pageInfos: NavPathStack; 180 181 build() { 182 NavDestination() { 183 Column() { 184 Text("NavDestinationContent3") 185 }.width('100%').height('100%') 186 }.title("NavDestinationTitle3") 187 .onBackPressed(() => { 188 const popDestinationInfo = this.pageInfos.pop() // Pop the top element out of the navigation stack. 189 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 190 return true 191 }) 192 } 193 } 194 ``` 195 196  197 198 199## Setting the Title Bar Mode 200 201The title bar is on the top of the page and is used to display the page name and operation entry. The **Navigation** component uses the **titleMode** attribute to set the title bar mode. 202 203> **NOTE** 204> 205> If no main title or subtitle is set for **Navigation** or **NavDestination** and there is no back button, the title bar is not displayed. 206 207- Mini mode 208 209 Applicable when the title of a level-1 page does not need to be highlighted. 210 211 **Figure 3** Title bar in Mini mode 212 213  214 215 216 ```ts 217 Navigation() { 218 // ... 219 } 220 .titleMode(NavigationTitleMode.Mini) 221 ``` 222 223 224- Full mode 225 226 Applicable when the title of a level-1 page needs to be highlighted. 227 228 **Figure 4** Title bar in Full mode 229 230  231 232 233 ```ts 234 Navigation() { 235 // ... 236 } 237 .titleMode(NavigationTitleMode.Full) 238 ``` 239 240 241## Setting the Menu Bar 242 243The menu bar is in the upper right corner of the **Navigation** component. You can set the menu bar through the **menus** attribute, which supports two parameter types: Array<[NavigationMenuItem](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#navigationmenuitem)> and [CustomBuilder](../reference/apis-arkui/arkui-ts/ts-types.md#custombuilder8). When the Array\<NavigationMenuItem> type is used, a maximum of three icons can be displayed in portrait mode and a maximum of five icons can be displayed in landscape mode. Extra icons will be placed in the automatically generated More icons. 244 245**Figure 5** Menu bar with three icons 246 247 248 249```ts 250let TooTmp: NavigationMenuItem = {'value': "", 'icon': "./image/ic_public_highlights.svg", 'action': ()=> {}} 251Navigation() { 252 // ... 253} 254.menus([TooTmp, 255 TooTmp, 256 TooTmp]) 257``` 258 259You can also reference images in the **resources** folder. 260 261```ts 262let TooTmp: NavigationMenuItem = {'value': "", 'icon': "resources/base/media/ic_public_highlights.svg", 'action': ()=> {}} 263Navigation() { 264 // ... 265} 266.menus([TooTmp, 267 TooTmp, 268 TooTmp]) 269``` 270 271**Figure 6** Menu bar with four icons 272 273 274 275```ts 276let TooTmp: NavigationMenuItem = {'value': "", 'icon': "./image/ic_public_highlights.svg", 'action': ()=> {}} 277Navigation() { 278 // ... 279} 280// In portrait mode, the toolbar shows a maximum of five icons, with any additional icons placed under an automatically generated More icon. 281.menus([TooTmp, 282 TooTmp, 283 TooTmp, 284 TooTmp]) 285``` 286 287 288## Setting the Toolbar 289 290Use the [toolbarConfiguration](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#toolbarconfiguration10) attribute to customize the toolbar, which is located at the bottom of the **Navigation** component. 291 292 293 **Figure 7** Toolbar 294 295 296 297```ts 298let TooTmp: ToolbarItem = {'value': "func", 'icon': "./image/ic_public_highlights.svg", 'action': ()=> {}} 299let TooBar: ToolbarItem[] = [TooTmp,TooTmp,TooTmp] 300Navigation() { 301 // ... 302} 303.toolbarConfiguration(TooBar) 304``` 305 306## Routing Operations 307 308Navigation-related routing operations are all based on the APIs provided by [NavPathStack](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#navpathstack10). For each **Navigation** component, a **NavPathStack** object must be created and passed in to manage pages. The router operations mainly involve page navigation, page return, page replacement, page deletion, parameter acquisition, and route interception. 309 310**NavPathStack** can be inherited since API version 12. You can customize attributes and methods in derived classes or override methods of the parent class. Derived class objects can be used in place of the base class **NavPathStack** objects. For details about the sample code, see [Example 10: Defining a Derived Class of NavPathStack](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#example-10-defining-a-derived-class-of-navpathstack). 311 312> **NOTE** 313> 314> Avoid relying on lifecycle event listeners as a means to manage the navigation stack. 315 316```ts 317@Entry 318@Component 319struct Index { 320 // Create a NavPathStack object and pass it to Navigation. 321 pageStack: NavPathStack = new NavPathStack() 322 323 build() { 324 Navigation(this.pageStack) { 325 } 326 .title('Main') 327 } 328} 329``` 330 331### Page Navigation 332 333**NavPathStack** implements the following types of page navigation through **Push** related APIs: 334 3351. Normal navigation: Navigation is conducted by page name and allows for passing of **param**. 336 337 ```ts 338 this.pageStack.pushPath({ name: "PageOne", param: "PageOne Param" }) 339 this.pageStack.pushPathByName("PageOne", "PageOne Param") 340 ``` 341 3422. Navigation with a return callback: An **onPop** callback is added during navigation to obtain return information and process it upon page popping. 343 344 ```ts 345 this.pageStack.pushPathByName('PageOne', "PageOne Param", (popInfo) => { 346 console.log('Pop page name is: ' + popInfo.info.name + ', result: ' + JSON.stringify(popInfo.result)) 347 }); 348 ``` 349 3503. Navigation with an error code: Upon failure, an asynchronous callback is triggered to provide the error code information. 351 352 ```ts 353 this.pageStack.pushDestination({name: "PageOne", param: "PageOne Param"}) 354 .catch((error: BusinessError) => { 355 console.error(`Push destination failed, error code = ${error.code}, error.message = ${error.message}.`); 356 }).then(() => { 357 console.info('Push destination succeed.'); 358 }); 359 this.pageStack.pushDestinationByName("PageOne", "PageOne Param") 360 .catch((error: BusinessError) => { 361 console.error(`Push destination failed, error code = ${error.code}, error.message = ${error.message}.`); 362 }).then(() => { 363 console.info('Push destination succeed.'); 364 }); 365 ``` 366 367### Page Return 368 369**NavPathStack** implements the page return feature through **Pop** related APIs. 370 371```ts 372// Return to the previous page. 373this.pageStack.pop() 374// Return to the previous PageOne page. 375this.pageStack.popToName("PageOne") 376// Return to the page whose index is 1. 377this.pageStack.popToIndex(1) 378// Return to the root home page (clear all pages in the stack). 379this.pageStack.clear() 380``` 381 382### Page Replacement 383 384**NavPathStack** implements the page replacement feature through **Replace** related APIs. 385 386```ts 387// Replace the top page of the stack with PageOne. 388this.pageStack.replacePath({ name: "PageOne", param: "PageOne Param" }) 389this.pageStack.replacePathByName("PageOne", "PageOne Param") 390// Replacement with an error code: Upon failure, an asynchronous callback is triggered to provide the error code information. 391this.pageStack.replaceDestination({name: "PageOne", param: "PageOne Param"}) 392 .catch((error: BusinessError) => { 393 console.error(`Replace destination failed, error code = ${error.code}, error.message = ${error.message}.`); 394 }).then(() => { 395 console.info('Replace destination succeed.'); 396 }) 397``` 398 399### Page Deletion 400 401**NavPathStack** implements the page deletion feature through **Remove** related APIs. 402 403```ts 404// Remove all pages whose name is PageOne from the stack. 405this.pageStack.removeByName("PageOne") 406// Remove the page with the specified index. 407this.pageStack.removeByIndexes([1,3,5]) 408// Remove the page with the specified ID. 409this.pageStack.removeByNavDestinationId("1"); 410``` 411 412### Page Moving 413 414**NavPathStack** implements the page moving feature through **Move** related APIs. 415 416```ts 417// Move the page named PageOne to the top of the stack. 418this.pageStack.moveToTop("PageOne"); 419// Move the page at index 1 to the top of the stack. 420this.pageStack.moveIndexToTop(1); 421``` 422 423### Parameter Acquisition 424 425**NavPathStack** obtains parameters of the page through **Get** related APIs. 426 427```ts 428// Obtain all page names in the stack. 429this.pageStack.getAllPathName() 430// Obtain the parameters of the page whose index is 1. 431this.pageStack.getParamByIndex(1) 432// Obtain the parameters of the PageOne page. 433this.pageStack.getParamByName("PageOne") 434// Obtain the index set of the PageOne page. 435this.pageStack.getIndexByName("PageOne") 436``` 437 438### Route Interception 439 440**NavPathStack** provides the [setInterception](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#setinterception12) API to set callbacks for page navigation interception of **Navigation**. This API requires passing in a **NavigationInterception** object, which contains three callback functions described below. 441 442| Name | Description | 443| ------------ | ------------------------------------------------------ | 444| willShow | Callback invoked when the page is about to be navigated, allowing for stack operations, which are effective in the current navigation. | 445| didShow | Callback invoked after the page is navigated. Stack operations in this callback are effective in the next navigation.| 446| modeChange | Callback invoked when the display mode of the **Navigation** component switches between single-column and dual-column. | 447 448> **NOTE** 449> 450> The navigation stack has already changed when any of the preceding callbacks is invoked. 451 452You can implement the capability to intercept and redirect in the **willShow** callback by modifying the route stack. 453 454```ts 455this.pageStack.setInterception({ 456 willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar", 457 operation: NavigationOperation, animated: boolean) => { 458 if (typeof to === "string") { 459 console.log("target page is navigation home page."); 460 return; 461 } 462 // Redirect navigation to PageTwo to PageOne. 463 let target: NavDestinationContext = to as NavDestinationContext; 464 if (target.pathInfo.name === 'PageTwo') { 465 target.pathStack.pop(); 466 target.pathStack.pushPathByName('PageOne', null); 467 } 468 } 469}) 470``` 471 472## Subpage 473 474[NavDestination](../reference/apis-arkui/arkui-ts/ts-basic-components-navdestination.md) is the root container for **Navigation** subpages, used to hold some special attributes as well as lifecycles of subpages. You can set separate attributes such as the title bar and menu bar for **NavDestination**, in the same way you set attributes for **Navigation**. You can also set different display modes for **NavDestination** through the **mode** attribute to meet different page requirements. 475 476### Page Display Mode 477 478- Standard mode 479 480 By default, subpages in the **NavDestination** component are in standard mode, which corresponds to the **NavDestinationMode.STANDARD** value of the **mode** attribute. The lifecycle of a standard type **NavDestination** follows the changes in its position in the **NavPathStack**. 481 482- Dialog mode 483 484 With **mode** set to **NavDestinationMode.DIALOG**, a **NavDestination** The appearance and disappearance of the dialog-type **NavDestination** will not affect the display and lifecycle of the underlying standard-type **NavDestination**, and the two can be displayed at the same time. 485 486 ```ts 487 // Dialog NavDestination 488 @Entry 489 @Component 490 struct Index { 491 @Provide('NavPathStack') pageStack: NavPathStack = new NavPathStack() 492 493 @Builder 494 PagesMap(name: string) { 495 if (name == 'DialogPage') { 496 DialogPage() 497 } 498 } 499 500 build() { 501 Navigation(this.pageStack) { 502 Button('Push DialogPage') 503 .margin(20) 504 .width('80%') 505 .onClick(() => { 506 this.pageStack.pushPathByName('DialogPage', ''); 507 }) 508 } 509 .mode(NavigationMode.Stack) 510 .title('Main') 511 .navDestination(this.PagesMap) 512 } 513 } 514 515 @Component 516 export struct DialogPage { 517 @Consume('NavPathStack') pageStack: NavPathStack; 518 519 build() { 520 NavDestination() { 521 Stack({ alignContent: Alignment.Center }) { 522 Column() { 523 Text("Dialog NavDestination") 524 .fontSize(20) 525 .margin({ bottom: 100 }) 526 Button("Close").onClick(() => { 527 this.pageStack.pop() 528 }).width('30%') 529 } 530 .justifyContent(FlexAlign.Center) 531 .backgroundColor(Color.White) 532 .borderRadius(10) 533 .height('30%') 534 .width('80%') 535 }.height("100%").width('100%') 536 } 537 .backgroundColor('rgba(0,0,0,0.5)') 538 .hideTitleBar(true) 539 .mode(NavDestinationMode.DIALOG) 540 } 541 } 542 ``` 543  544 545### Page Lifecycle 546 547**Navigation**, as a routing container, hosts its lifecycle within the **NavDestination** component and exposes lifecycle events as component events. 548 549The lifecycle of **Navigation** can be divided into three categories: custom component lifecycle, universal component lifecycle, and its own exclusive lifecycle. [aboutToAppear](../reference/apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#abouttoappear) and [aboutToDisappear](../reference/apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#abouttodisappear) are the lifecycle callbacks of custom components (custom components contained in the outer layer of **NavDestination**); [OnAppear](../reference/apis-arkui/arkui-ts/ts-universal-events-show-hide.md#onappear) and [OnDisappear](../reference/apis-arkui/arkui-ts/ts-universal-events-show-hide.md#ondisappear) are universal component lifecycle callbacks. The remaining six lifecycle events are unique to **NavDestination**. 550 551The sequence of these lifecycle events is illustrated in the figure below. 552 553 554 555- **aboutToAppear**: Invoked when the custom component is about to appear. Specifically, it is invoked after a new instance of the custom component is created and before its **build()** function is executed (before the creation of **NavDestination**). You can change state variables in this callback, and the changes take effect in the subsequent execution of **build()**. 556- **onWillAppear**: invoked after the **NavDestination** component is created and before it is mounted to the component tree. Changing the state variable in this callback takes effect in the current frame. 557- **onAppear**: invoked when the **NavDestination** component is mounted to the component tree. It is a universal lifecycle event. 558- **onWillShow**: invoked before the **NavDestination** component layout is displayed. In this case, the page is invisible. (This callback is not invoked when the application is switched to the foreground.) 559- **onShown**: invoked after the **NavDestination** component layout is displayed. At this time, the page layout is complete. 560- **onWillHide**: invoked when the **NavDestination** component is about to be hidden (it is not invoked when the application is switched to the background). 561- **onHidden**: invoked after the **NavDestination** component is hidden (when a non-top page is pushed into the stack, the top page pops out of the stack, or the application is switched to the background). 562- **onWillDisappear**: invoked before the **NavDestination** component is about to be destroyed. If there is a transition animation, this callback is invoked before the animation (when the top page of the stack pops out of the stack). 563- **onDisappear**: invoked when the **NavDestination** component is unloaded and destroyed from the component tree. It is a universal lifecycle event. 564- **aboutToDisappear**: invoked before the custom component is destroyed. The state variable cannot be changed in this callback. 565 566### Page Listening and Query 567 568To facilitate the decoupling of components from pages, custom components within **NavDestination** subpages can listen for or query some page status information through global APIs. 569 570- Page information query 571 572 Custom components provide the [queryNavDestinationInfo](../reference/apis-arkui/arkui-ts/ts-custom-component-api.md#querynavdestinationinfo) API, which can be used to query the information of the current page within **NavDestination**. The return value is [NavDestinationInfo](../reference/apis-arkui/js-apis-arkui-observer.md#navdestinationinfo). If the query fails, the return value is **undefined**. 573 574 ```ts 575 import { uiObserver } from '@kit.ArkUI'; 576 577 // Custom components within NavDestination 578 @Component 579 struct MyComponent { 580 navDesInfo: uiObserver.NavDestinationInfo | undefined 581 582 aboutToAppear(): void { 583 this.navDesInfo = this.queryNavDestinationInfo(); 584 } 585 586 build() { 587 Column() { 588 Text("Page name: " + this.navDesInfo?.name) 589 }.width('100%').height('100%') 590 } 591 } 592 ``` 593- Page status listening 594 595 You can register a listener for **NavDestination** lifecycle changes using the [observer.on('navDestinationUpdate')](../reference/apis-arkui/js-apis-arkui-observer.md#uiobserveronnavdestinationupdate) API. 596 597 ```ts 598 uiObserver.on('navDestinationUpdate', (info) => { 599 console.info('NavDestination state update', JSON.stringify(info)); 600 }); 601 ``` 602 603 You can also register a callback for page transition states to obtain page information during route changes using [NavDestinationSwitchInfo](../reference/apis-arkui/js-apis-arkui-observer.md#navdestinationswitchinfo12). This registration supports different scopes: UIAbilityContext and UIContext. 604 605 ```ts 606 // Used in UIAbility 607 import { UIContext, uiObserver } from '@kit.ArkUI'; 608 609 // callBackFunc is a callback defined by you. 610 function callBackFunc(info: uiObserver.NavDestinationSwitchInfo) {} 611 uiObserver.on('navDestinationSwitch', this.context, callBackFunc); 612 613 // The getUIContext() API of the window can be used to obtain the corresponding UIContent. 614 uiContext: UIContext | null = null; 615 uiObserver.on('navDestinationSwitch', this.uiContext, callBackFunc); 616 ``` 617 618## Page Transition 619 620The **Navigation** component provides default transition animations for switching between pages. These animations are activated when operations are performed on the navigation stack, producing different transition effects. Note that for dialog box pages, the default transition animations are available only since API version 13. The **Navigation** component also offers advanced features such as disabling the default transitions and implementing custom transitions as well as shared element transitions. 621 622### Disabling Transitions 623 624- On a Global Basis 625 626 To enable or disable all transition animations in the current **Navigation** component, you can use the [disableAnimation](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#disableanimation11) API provided in **NavPathStack**. 627 ```ts 628 pageStack: NavPathStack = new NavPathStack() 629 630 aboutToAppear(): void { 631 this.pageStack.disableAnimation(true) 632 } 633 ``` 634- On a One-time Basis 635 636 To disable the transition animation for a single operation (implemented by APIs provided by **NavPathStack**, such as **Push**, **Pop**, and **Replace**), set the **animated** parameter in the API to **false**. This setting does not affect the next transition. 637 ```ts 638 pageStack: NavPathStack = new NavPathStack() 639 640 this.pageStack.pushPath({ name: "PageOne" }, false) 641 this.pageStack.pop(false) 642 ``` 643 644### Customizing a Transition 645 646You can customize transition animations through the [customNavContentTransition](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#customnavcontenttransition11) event. Specifically, you can define a custom transition animation in the following steps: 647 6481. Build a custom transition animation utility class **CustomNavigationUtils**, which manages custom transition animation **CustomTransition** objects for each page through a Map. A page registers its **CustomTransition** object when created and unregisters it when destroyed. 6492. Implement a transition protocol object [NavigationAnimatedTransition](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#navigationanimatedtransition11). The **timeout** property indicates the transition timeout duration, with a default value of 1000 ms. The **transition** property is where you implement your custom transition animation logic; it is the method that the system calls when the transition starts. The **onTransitionEnd** is the callback for when the transition ends. 6503. Call the **customNavContentTransition** API to return the **NavigationAnimatedTransition** object. If **undefined** is returned, the system default transition is used. 651 652For details about the sample code, see [Example 3: Setting an Interactive Transition Animation](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#example-3-setting-an-interactive-transition-animation). 653 654### Defining a Shared Element Transition 655 656You can implement shared element transitions between navigation destination pages using [geometryTransition](../reference/apis-arkui/arkui-ts/ts-transition-animation-geometrytransition.md#geometrytransition). Ensure that the default transition animations are disabled for pages configured with shared element transitions. 6571. Add the **geometryTransition** attribute to the components that need to implement shared element transitions, ensuring that the **id** parameter is consistent between the two **NavDestination** components. 658 659 ```ts 660 // Set id of the source page. 661 NavDestination() { 662 Column() { 663 // ... 664 Image($r('app.media.startIcon')) 665 .geometryTransition('sharedId') 666 .width(100) 667 .height(100) 668 } 669 } 670 .title('FromPage') 671 672 // Set id of the destination page. 673 NavDestination() { 674 Column() { 675 // ... 676 Image($r('app.media.startIcon')) 677 .geometryTransition('sharedId') 678 .width(200) 679 .height(200) 680 } 681 } 682 .title('ToPage') 683 ``` 684 6852. Place the page routing operation in the **animateTo** animation closure, set the corresponding animation parameters, and disable the default transition. 686 687 ```ts 688 NavDestination() { 689 Column() { 690 Button('Go to Destination Page') 691 .width('80%') 692 .height(40) 693 .margin(20) 694 .onClick(() => { 695 this.getUIContext()?.animateTo({ duration: 1000 }, () => { 696 this.pageStack.pushPath({ name: 'ToPage' }, false) 697 }) 698 }) 699 } 700 } 701 .title('FromPage') 702 ``` 703 704## Cross-Package Dynamic Routing 705 706Using static imports for page routing can lead to coupling between different modules and prolonged home page loading times. 707 708The purpose of dynamic routing is to allow multiple modules (HARs/HSPs) to reuse the same business logic, with decoupling between different modules and integration of routing functionality. 709 710**Advantages of dynamic routing** 711 712- In addition to the URL for navigation, you can configure various information, such as the default orientation (landscape or portrait) and whether authentication is required. The configuration is processed in a unified manner during routing 713- You can assign a name to each routing page and navigate by name instead of file path. 714- Dynamic imports (on-demand loading) can be used to load pages to prevent the first page from loading a large amount of code, which can cause stuttering. 715 716Dynamic routing provides two modes: [System Routing Table](#system-routing-table) and [Custom Routing Table](#custom-routing-table). 717 718- The system routing table is easier to use than the custom routing table. It only requires adding the corresponding page navigation configuration items to implement page navigation. 719 720- The custom route table is more complex to use, but can be customized to meet specific service requirements. 721 722The custom route table and system route table can be used together. 723 724### System Routing Table 725 726**Navigation** supports the system routing table for dynamic routing since API version 12. Each service module ([HSP](../quick-start/in-app-hsp.md) or [HAR](../quick-start/har-package.md)) requires an individual **route_map.json** file. When routing is triggered, the application only needs to pass the name of the page that needs to be routed through the routing API provided by **NavPathStack**. The system then automatically completes the dynamic loading of the target module, page component construction, and route redirection. This way, module decoupling is achieved at the development level. Note that the system routing table does not work with DevEco Studio Previewer, cross-platform functionality, or emulators. The main steps are as follows: 727 7281. Add the route table configuration to the **module.json5** file of the redirection target module. 729 730 ```json 731 { 732 "module" : { 733 "routerMap": "$profile:route_map" 734 } 735 } 736 ``` 7372. Create the **route_map.json** file in **resources/base/profile** of the project directory. Add the following configuration information: 738 739 ```json 740 { 741 "routerMap": [ 742 { 743 "name": "PageOne", 744 "pageSourceFile": "src/main/ets/pages/PageOne.ets", 745 "buildFunction": "PageOneBuilder", 746 "data": { 747 "description" : "this is PageOne" 748 } 749 } 750 ] 751 } 752 ``` 753 754 The configuration is described as follows. 755 756 | Item| Description| 757 |---|---| 758 | name | Name of the target page to be redirected to.| 759 | pageSourceFile | Path of the target page in the package, relative to the **src** directory.| 760 | buildFunction | Name of the entry point function for redirection to the target page, which must be decorated by @Builder.| 761 | data | Custom data. You can obtain the value through the **getConfigInRouteMap** API.| 762 7633. On the target page, configure the @Builder decorated entry point function. The function name must be the same as the value of **buildFunction** in the **route_map.json** file. Otherwise, an error is reported at compile time. 764 765 ```ts 766 // Entry point function for redirection to the target page 767 @Builder 768 export function PageOneBuilder() { 769 PageOne() 770 } 771 772 @Component 773 struct PageOne { 774 pathStack: NavPathStack = new NavPathStack() 775 776 build() { 777 NavDestination() { 778 } 779 .title('PageOne') 780 .onReady((context: NavDestinationContext) => { 781 this.pathStack = context.pathStack 782 }) 783 } 784 } 785 ``` 7864. Use routing APIs such as **pushPathByName** to navigate to the target page. (Note: In this case, you do not need to configure the **navDestination** attribute in **Navigation**.) 787 788 ```ts 789 @Entry 790 @Component 791 struct Index { 792 pageStack : NavPathStack = new NavPathStack(); 793 794 build() { 795 Navigation(this.pageStack){ 796 }.onAppear(() => { 797 this.pageStack.pushPathByName("PageOne", null, false); 798 }) 799 .hideNavBar(true) 800 } 801 } 802 ``` 803 804### Custom Routing Table 805 806You can implement cross-package dynamic routing through a custom routing table. 807 808**Implementation:** 809 8101. Define page navigation configuration items. 811 - Use resource files for definitions and use the [@ohos.resourceManager](../reference/apis-localization-kit/js-apis-resource-manager.md) module to parse the resource files at runtime. 812 - Configure the route loading options in the .ets file, including the route page name (that is, the alias of the page in APIs like **pushPath**), name of the module where the file is located (name of the HSP/HAR module), and path to the target page in the module (relative to the **src** directory). 8132. Use [dynamic import](../arkts-utils/arkts-dynamic-import.md) to load the module containing the target page at runtime. Once the module is loaded, call a method within the module that uses **import** to load and display the target page, then return the **Builder** function defined after the page has finished loading. 8143. Execute the **Builder** function loaded in step 2 on the [navDestination](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md#navdestination10) attribute of the **Navigation** component to navigate to the target page. 815<!--RP2--><!--RP2End--> 816