1# NavRouter
2
3导航组件,默认提供点击响应处理,不需要开发者自定义点击事件逻辑。
4
5> **说明:**
6>
7> 从API Version 13 开始,该组件不再维护,推荐使用[NavPathStack](ts-basic-components-navigation.md#navpathstack10)配合navDestination属性进行页面路由。
8>
9> 该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
10
11## 子组件
12
13必须包含两个子组件,其中第二个子组件必须为[NavDestination](ts-basic-components-navdestination.md)。
14
15> **说明:**
16>
17> 子组件个数异常时:
18> 1. 有且仅有1个时,触发路由到NavDestination的能力失效。
19> 2. 有且仅有1个时,且使用NavDestination场景下,不进行路由。
20> 3. 大于2个时,后续的子组件不显示。
21> 4. 第二个子组件不为NavDestination时,触发路由功能失效。
22
23## 接口
24
25### NavRouter
26
27NavRouter()
28
29**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
30
31**系统能力:** SystemCapability.ArkUI.ArkUI.Full
32
33### NavRouter<sup>10+</sup>
34
35NavRouter(value: RouteInfo)
36
37提供路由信息,指定点击NavRouter时,要跳转的NavDestination页面。
38
39**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
40
41**系统能力:** SystemCapability.ArkUI.ArkUI.Full
42
43**参数:**
44
45| 参数名     | 类型                                | 必填   | 说明          |
46| ------- | ----------------------------------- | ---- | ------------- |
47| value   | [RouteInfo](#routeinfo10对象说明) | 是    | 路由信息 |
48
49## 属性
50
51除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
52
53### mode<sup>10+</sup>
54
55mode(mode: NavRouteMode)
56
57设置指定点击NavRouter跳转到NavDestination页面时,使用的路由模式。
58
59**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
60
61**系统能力:** SystemCapability.ArkUI.ArkUI.Full
62
63**参数:**
64
65| 参数名                           | 类型                                     | 必填                                   | 说明                                       |
66| ----------------------------- | ---------------------------------------- | ---------------------------------------- | ---------------------------------------- |
67| mode                  | [NavRouteMode](#navroutemode10枚举说明)                                  | 是                                 | 指定点击NavRouter跳转到NavDestination页面时,使用的路由模式。<br/>默认值:NavRouteMode.PUSH_WITH_RECREATE |
68
69## RouteInfo<sup>10+</sup>对象说明
70
71**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
72
73**系统能力:** SystemCapability.ArkUI.ArkUI.Full
74
75| 名称                 | 类型                                                     | 必填 | 说明                                                         |
76| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
77| name             | string            | 是   | 点击NavRouter跳转到的NavDestination页面的名称。 |
78| param             | unknown            | 否   | 点击NavRouter跳转到NavDestination页面时,传递的参数。 |
79
80## NavRouteMode<sup>10+</sup>枚举说明
81
82**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
83
84**系统能力:** SystemCapability.ArkUI.ArkUI.Full
85
86| 名称    | 说明             |
87| ----- | ---------------- |
88| PUSH_WITH_RECREATE | 跳转到新的NavDestination页面时,替换当前显示的NavDestination页面,页面销毁,但该页面信息仍保留在路由栈中。 |
89| PUSH   | 跳转到新的NavDestination页面时,覆盖当前显示的NavDestination页面,该页面不销毁,且页面信息保留在路由栈中。 |
90| REPLACE   | 跳转到新的NavDestination页面时,替换当前显示的NavDestination页面,页面销毁,且该页面信息从路由栈中清除。 |
91
92## 事件
93
94### onStateChange
95
96onStateChange(callback: (isActivated: boolean) => void)
97
98组件激活状态切换时触发该回调。开发者点击激活NavRouter,加载对应的NavDestination子组件时,回调onStateChange(true)。NavRouter对应的NavDestination子组件不再显示时,回调onStateChange(false)。
99
100**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
101
102**系统能力:** SystemCapability.ArkUI.ArkUI.Full
103
104**参数:**
105
106| 参数名      | 类型    | 必填 | 说明                                    |
107| ----------- | ------- | ---- | --------------------------------------- |
108| isActivated | boolean | 是   | isActivated为true时表示激活,为false时表示未激活。 |
109
110## 示例
111
112```ts
113// xxx.ets
114@Entry
115@Component
116struct NavRouterExample {
117  @State isActiveWLAN: boolean = false
118  @State isActiveBluetooth: boolean = false
119
120  build() {
121    Navigation() {
122      NavRouter() {
123        Row() {
124          Row()
125            .width(30)
126            .height(30)
127            .borderRadius(30)
128            .margin({ left: 3, right: 10 })
129            .backgroundColor(Color.Pink)
130          Text(`WLAN`)
131            .fontSize(22)
132            .fontWeight(500)
133            .textAlign(TextAlign.Center)
134        }
135        .width('90%')
136        .height(60)
137
138        NavDestination() {
139          Flex({ direction: FlexDirection.Row }) {
140            Text('未找到可用WLAN').fontSize(30).padding({ left: 15 })
141          }
142        }.title("WLAN")
143      }
144      .margin({ top: 10, bottom: 10 })
145      .backgroundColor(this.isActiveWLAN ? '#ccc' : '#fff')
146      .borderRadius(20)
147      .mode(NavRouteMode.PUSH_WITH_RECREATE)
148      .onStateChange((isActivated: boolean) => {
149        this.isActiveWLAN = isActivated
150      })
151
152      NavRouter() {
153        Row() {
154          Row()
155            .width(30)
156            .height(30)
157            .borderRadius(30)
158            .margin({ left: 3, right: 10 })
159            .backgroundColor(Color.Pink)
160          Text(`蓝牙`)
161            .fontSize(22)
162            .fontWeight(500)
163            .textAlign(TextAlign.Center)
164        }
165        .width('90%')
166        .height(60)
167
168        NavDestination() {
169          Flex({ direction: FlexDirection.Row }) {
170            Text('未找到可用蓝牙').fontSize(30).padding({ left: 15 })
171          }
172        }.title("蓝牙")
173      }
174      .margin({ top: 10, bottom: 10 })
175      .backgroundColor(this.isActiveBluetooth ? '#ccc' : '#fff')
176      .borderRadius(20)
177      .mode(NavRouteMode.REPLACE)
178      .onStateChange((isActivated: boolean) => {
179        this.isActiveBluetooth = isActivated
180      })
181    }
182    .height('100%')
183    .width('100%')
184    .title('设置')
185    .backgroundColor("#F2F3F5")
186    .titleMode(NavigationTitleMode.Free)
187    .mode(NavigationMode.Auto)
188  }
189}
190```
191
192![NavRouter](./figures/NavRouter.gif)