• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# NavRouter
2
3The **\<NavRouter>** component provides default logic for click response processing, 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## APIs
14
15NavRouter()
16
17
18## Events
19
20| Name                                      | Description                                    |
21| ---------------------------------------- | ---------------------------------------- |
22| onStateChange(callback: (isActivated: boolean) => void) | Invoked when the component activation status changes. The value **true** means that component is activated, and **false** means the opposite.<br> **NOTE**<br>After the user clicks **NavRouter**, if the **\<NavRouter>** component is activated and the corresponding **\<NavDestination>** child component loaded, **onStateChange(true)** is called. If the corresponding **\<NavDestination>** child component is no longer displayed, **onStateChange(false)** is called. |
23
24## Example
25
26```ts
27// xxx.ets
28@Entry
29@Component
30struct NavRouterExample {
31  private arr: number[] = [0, 1, 2, 3]
32  @State isActive: boolean = false
33  @State dex: number = 0
34
35  build() {
36    Column() {
37      Navigation() {
38        List({ space: 12, initialIndex: 0 }) {
39          ForEach(this.arr, (item: number, index: number) => {
40            ListItem() {
41              NavRouter() {
42                Row() {
43                  Image($r('app.media.icon')).width(30).height(30).borderRadius(30).margin({ left: 3, right: 10 })
44                  Text(`NavRouter${item + 1}`)
45                    .fontSize(22)
46                    .fontWeight(500)
47                    .textAlign(TextAlign.Center)
48                }
49                .width(180)
50                .height(72)
51                .backgroundColor(this.dex === index ? '#ccc' : '#fff')
52                .borderRadius(24)
53
54                NavDestination() {
55                  Text (`I am NavDestination page ${item + 1}`).fontSize (50)
56                  Flex({ direction: FlexDirection.Row }) {
57                    Row() {
58                      Image($r('app.media.icon')).width(40).height(40).borderRadius(40).margin({ right: 15 })
59                      Text('7 classes today').fontSize(30)
60                    }.padding({ left: 15 })
61                  }
62                }.backgroundColor('#ccc')
63                .title(`NavDestination${item + 1}`)
64              }.onStateChange((isActivated: boolean) => {
65                this.dex = index
66              })
67            }
68          }, item => item)
69        }
70        .height('100%')
71        .margin({ top: 12, left: 12 })
72      }
73      .mode(NavigationMode.Split)
74      .hideTitleBar(true)
75      .hideToolBar(true)
76    }.height('100%')
77  }
78}
79```
80