• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Navigator
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @mayaolll-->
5<!--Designer: @jiangdayuan-->
6<!--Tester: @lxl007-->
7<!--Adviser: @HelloCrease-->
8
9路由容器组件,提供路由跳转能力。
10
11> **说明:**
12>
13> 从API version 13开始,该组件不再维护,推荐使用组件[Navigation](ts-basic-components-navigation.md)进行页面路由。
14>
15> 该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
16
17
18## 子组件
19
20可以包含子组件。
21
22
23## 接口
24
25Navigator(value?: {target: string, type?: NavigationType})
26
27**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
28
29**系统能力:** SystemCapability.ArkUI.ArkUI.Full
30
31**参数:**
32
33| 参数名 | 类型       | 必填 | 说明                                       |
34| ------ | -------------- | ---- | ---------------------------------------------- |
35| target | string         | 是   | 指定跳转目标页面的路径。     |
36| type   | [NavigationType](#navigationtype枚举说明) | 否   | 指定路由方式。<br/>默认值:NavigationType.Push |
37
38## NavigationType枚举说明
39
40**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
41
42**系统能力:** SystemCapability.ArkUI.ArkUI.Full
43
44| 名称      | 值  | 说明                       |
45| ------- | ------- | -------------------------- |
46| Push    | 1 | 跳转到应用内的指定页面。               |
47| Replace | 2 | 用应用内的某个页面替换当前页面,并销毁被替换的页面。 |
48| Back    | 3 | 返回到指定的页面。指定的页面不存在栈中时不响应。未传入指定的页面时返回上一页。 |
49
50## 属性
51
52### active
53
54active(value: boolean)
55
56设置当前路由组件是否处于激活状态,处于激活状态时,会生效相应的路由操作。
57
58**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
59
60**系统能力:** SystemCapability.ArkUI.ArkUI.Full
61
62**参数:**
63
64| 参数名 | 类型    | 必填 | 说明                       |
65| ------ | ------- | ---- | -------------------------- |
66| value  | boolean | 是   | 路由组件是否处于激活状态。设置为true时,组件处于激活态。设置为false时,组件不处于激活态。 |
67
68### params
69
70params(value: object)
71
72设置跳转时传递到目标页面的数据。
73
74**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
75
76**系统能力:** SystemCapability.ArkUI.ArkUI.Full
77
78**参数:**
79
80| 参数名 | 类型   | 必填 | 说明                                                         |
81| ------ | ------ | ---- | ------------------------------------------------------------ |
82| value  | object | 是   | 跳转时要同时传递到目标页面的数据,可在目标页面使用[router.getParams()](../js-apis-router.md#routergetparamsdeprecated)获得。 |
83
84### target
85
86target(value: string)
87
88设置跳转目标页面的路径。目标页面需加入main_pages.json文件中。
89
90**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
91
92**系统能力:** SystemCapability.ArkUI.ArkUI.Full
93
94**参数:**
95
96| 参数名 | 类型   | 必填 | 说明               |
97| ------ | ------ | ---- | ------------------ |
98| value  | string | 是   | 转目标页面的路径。 |
99
100### type
101type(value: NavigationType)
102
103设置路由跳转方式。
104
105**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
106
107**系统能力:** SystemCapability.ArkUI.ArkUI.Full
108
109**参数:**
110
111| 参数名 | 类型   | 必填 | 说明                                           |
112| ------ | ------ | ---- | ---------------------------------------------- |
113| value  | [NavigationType](#navigationtype枚举说明) | 是   | 路由跳转方式。<br/>默认值:NavigationType.Push |
114
115## 示例
116
117```ts
118// Navigator.ets
119@Entry
120@Component
121struct NavigatorExample {
122  @State active: boolean = false
123  @State name: NameObject = { name: 'news' }
124
125  build() {
126    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
127      Navigator({ target: 'pages/container/navigator/Detail', type: NavigationType.Push }) {
128        Text('Go to ' + this.name.name + ' page')
129          .width('100%').textAlign(TextAlign.Center)
130      }.params(new TextObject(this.name)) // 传参数到Detail页面
131
132      Navigator() {
133        Text('Back to previous page').width('100%').textAlign(TextAlign.Center)
134      }.active(this.active)
135      .onClick(() => {
136        this.active = true
137      })
138    }.height(150).width(350).padding(35)
139  }
140}
141
142interface NameObject {
143  name: string;
144}
145
146class TextObject {
147  text: NameObject;
148
149  constructor(text: NameObject) {
150    this.text = text;
151  }
152}
153```
154
155```ts
156// Detail.ets
157@Entry
158@Component
159struct DetailExample {
160  // 接收Navigator.ets的传参
161  params: Record<string, NameObject> = this.getUIContext().getRouter().getParams() as Record<string, NameObject>
162  @State name: NameObject = this.params.text
163
164  build() {
165    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
166      Navigator({ target: 'pages/container/navigator/Back', type: NavigationType.Push }) {
167        Text('Go to back page').width('100%').height(20)
168      }
169
170      Text('This is ' + this.name.name + ' page')
171        .width('100%').textAlign(TextAlign.Center)
172    }
173    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
174  }
175}
176
177interface NameObject {
178  name: string;
179}
180```
181
182```ts
183// Back.ets
184@Entry
185@Component
186struct BackExample {
187  build() {
188    Column() {
189      Navigator({ target: 'pages/container/navigator/Navigator', type: NavigationType.Back }) {
190        Text('Return to Navigator Page').width('100%').textAlign(TextAlign.Center)
191      }
192    }.width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
193  }
194}
195```
196
197![zh-cn_image_0000001219864145](figures/zh-cn_image_0000001219864145.gif)
198