• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Navigator
2
3路由容器组件,提供路由跳转能力。
4
5> **说明:**
6>
7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 子组件
11
12可以包含子组件。
13
14
15## 接口
16
17Navigator(value?: {target: string, type?: NavigationType})
18
19**参数:**
20
21| 参数名 | 参数类型       | 必填 | 参数描述                                       |
22| ------ | -------------- | ---- | ---------------------------------------------- |
23| target | string         | 是   | 指定跳转目标页面的路径。     |
24| type   | [NavigationType](#navigationtype枚举说明) | 否   | 指定路由方式。<br/>默认值:NavigationType.Push |
25
26## NavigationType枚举说明
27
28| 名称      | 枚举值  | 描述                         |
29| ------- | ------- | -------------------------- |
30| Push    | 1 | 跳转到应用内的指定页面。               |
31| Replace | 2 | 用应用内的某个页面替换当前页面,并销毁被替换的页面。 |
32| Back    | 3 | 返回到指定的页面。指定的页面不存在栈中时不响应。未传入指定的页面时返回上一页。 |
33
34
35## 属性
36
37| 名称   | 参数    | 描述                                                         |
38| ------ | ------- | ------------------------------------------------------------ |
39| active | boolean | 当前路由组件是否处于激活状态,处于激活状态时,会生效相应的路由操作。 |
40| params | object  | 跳转时要同时传递到目标页面的数据,可在目标页面使用[router.getParams()](../apis/js-apis-router.md#routergetparams)获得。 |
41| target | string         | 设置跳转目标页面的路径。 目标页面需加入main_pages.json文件中。                         |
42| type   | [NavigationType](#navigationtype枚举说明)  | 设置路由方式。<br/>默认值:NavigationType.Push |
43
44
45## 示例
46
47```ts
48// Navigator.ets
49@Entry
50@Component
51struct NavigatorExample {
52  @State active: boolean = false
53  @State name: NameObject = { name: 'news' }
54
55  build() {
56    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
57      Navigator({ target: 'pages/container/navigator/Detail', type: NavigationType.Push }) {
58        Text('Go to ' + this.name.name + ' page')
59          .width('100%').textAlign(TextAlign.Center)
60      }.params(new TextObject(this.name)) // 传参数到Detail页面
61
62      Navigator() {
63        Text('Back to previous page').width('100%').textAlign(TextAlign.Center)
64      }.active(this.active)
65      .onClick(() => {
66        this.active = true
67      })
68    }.height(150).width(350).padding(35)
69  }
70}
71
72interface NameObject {
73  name: string;
74}
75
76class TextObject {
77  text: NameObject;
78
79  constructor(text: NameObject) {
80    this.text = text;
81  }
82}
83```
84
85```ts
86// Detail.ets
87import router from '@ohos.router'
88
89@Entry
90@Component
91struct DetailExample {
92  // 接收Navigator.ets的传参
93  params: Record<string, NameObject> = router.getParams() as Record<string, NameObject>
94  @State name: NameObject = this.params.text
95
96  build() {
97    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
98      Navigator({ target: 'pages/container/navigator/Back', type: NavigationType.Push }) {
99        Text('Go to back page').width('100%').height(20)
100      }
101
102      Text('This is ' + this.name.name + ' page')
103        .width('100%').textAlign(TextAlign.Center)
104    }
105    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
106  }
107}
108
109interface NameObject {
110  name: string;
111}
112```
113
114```ts
115// Back.ets
116@Entry
117@Component
118struct BackExample {
119  build() {
120    Column() {
121      Navigator({ target: 'pages/container/navigator/Navigator', type: NavigationType.Back }) {
122        Text('Return to Navigator Page').width('100%').textAlign(TextAlign.Center)
123      }
124    }.width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
125  }
126}
127```
128
129![zh-cn_image_0000001219864145](figures/zh-cn_image_0000001219864145.gif)
130