1# NavRouter 2 3The **\<NavRouter>** component provides default processing logic for responding to clicks, eliminating the need for manual logic definition. 4 5> **NOTE** 6> 7> This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. 8 9## Child Components 10 11This component must contain two child components, the second of which must be **[\<NavDestination>](ts-basic-components-navdestination.md)**. 12 13> **NOTE** 14> 15> 16> 1. If there is only one child component, the navigation to the **\<NavDestination>** component does not work. 17> 2. If there is only the **\<NavDestination>** child component, the navigation does not work. 18> 3. If there are more than two child components, the excess child components are not displayed. 19> 4. If the second child component is not **\<NavDestination>**, the navigation does not work. 20 21## APIs 22 23### NavRouter 24 25NavRouter() 26 27### NavRouter<sup>10+</sup> 28 29NavRouter(value: RouteInfo) 30 31Provides route information so that clicking the **\<NavRouter>** component redirects the user to the specified navigation destination page. 32 33 34**Parameters** 35 36| Name | Type | Mandatory | Description | 37| ------- | ----------------------------------- | ---- | ------------- | 38| value | [RouteInfo](#routeinfo10) | No | Route information.| 39 40## Attributes 41 42In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 43 44| Name | Type | Description | 45| ----------------------------- | ---------------------------------------- | ---------------------------------------- | 46| mode | [NavRouteMode](#navroutemode) | Route mode used for redirection.<br>Default value: **NavRouteMode.PUSH_WITH_RECREATE** | 47 48## RouteInfo<sup>10+</sup> 49 50| Name | Type | Mandatory| Description | 51| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 52| name | string | Yes | Name of the navigation destination page to be redirected to.| 53| param | unknown | No | Parameter transferred during redirection.| 54 55## NavRouteMode 56 57| Name | Description | 58| ----- | ---------------- | 59| PUSH_WITH_RECREATE | The new navigation destination page replaces the current one. The current page is destroyed, but the information about this page is retained in the navigation stack.| 60| PUSH | The new navigation destination page overwrites the current one. The current page is not destroyed, and the information about this page is retained in the navigation stack.| 61| REPLACE | The new navigation destination page replaces the current one. The current page is destroyed, and the information about this page is removed from the navigation stack.| 62 63## Events 64 65| Name | Description | 66| ------------------------------------------------------- | ------------------------------------------------------------ | 67| onStateChange(callback: (isActivated: boolean) => void) | Called when the component activation status changes. The value **true** means that component is activated, and **false** means the opposite.<br>**NOTE**<br>**onStateChange(true)** is called when the **\<NavRouter>** component is activated and its **\<NavDestination>** child component is loaded. **onStateChange(false)** is called when the **\<NavDestination>** child component is not displayed. | 68 69## Example 70 71```ts 72// xxx.ets 73@Entry 74@Component 75struct NavRouterExample { 76 private arr: number[] = [0, 1, 2, 3] 77 @State isActive: boolean = false 78 @State dex: number = 0 79 80 build() { 81 Column() { 82 Navigation() { 83 List({ space: 12, initialIndex: 0 }) { 84 ForEach(this.arr, (item: number, index: number = 0) => { 85 ListItem() { 86 NavRouter() { 87 Row() { 88 Image($r('app.media.icon')).width(30).height(30).borderRadius(30).margin({ left: 3, right: 10 }) 89 Text(`NavRouter${item + 1}`) 90 .fontSize(22) 91 .fontWeight(500) 92 .textAlign(TextAlign.Center) 93 } 94 .width(180) 95 .height(72) 96 .backgroundColor(this.dex === index ? '#ccc' : '#fff') 97 .borderRadius(24) 98 99 NavDestination() { 100 Text (`I am NavDestination page ${item + 1}`).fontSize (50) 101 Flex({ direction: FlexDirection.Row }) { 102 Row() { 103 Image($r('app.media.icon')).width(40).height(40).borderRadius(40).margin({ right: 15 }) 104 Text('7 classes today').fontSize(30) 105 }.padding({ left: 15 }) 106 } 107 }.backgroundColor('#ccc') 108 .title(`NavDestination${item + 1}`) 109 }.onStateChange((isActivated: boolean) => { 110 this.dex = index 111 }) 112 } 113 }, (item:number) => item.toString()) 114 } 115 .height('100%') 116 .margin({ top: 12, left: 12 }) 117 } 118 .mode(NavigationMode.Split) 119 .hideTitleBar(true) 120 .hideToolBar(true) 121 }.height('100%') 122 } 123} 124``` 125