• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Navigator
2
3The **\<Navigator>** component provides redirection.
4
5> **NOTE**
6>
7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9
10## Child Components
11
12Supported
13
14
15## APIs
16
17Navigator(value?: {target: string, type?: NavigationType})
18
19**Parameters**
20
21| Name| Type      | Mandatory| Description                                      |
22| ------ | -------------- | ---- | ---------------------------------------------- |
23| target | string         | Yes  | Path of the target page to be redirected to.    |
24| type   | [NavigationType](#navigationtype) | No  | Navigation type.<br>Default value: **NavigationType.Push**|
25
26## NavigationType
27
28| Name     | Value | Description                        |
29| ------- | ------- | -------------------------- |
30| Push    | 1 | Navigates to the specified page in the application.              |
31| Replace | 2 | Replaces the current page with another one in the application and destroys the current page.|
32| Back    | 3 | Returns to the specified page. If the specified page does not exist in the stack, no response is returned. If no page is specified, the previous page is returned to.|
33
34
35## Attributes
36
37| Name  | Parameter   | Description                                                        |
38| ------ | ------- | ------------------------------------------------------------ |
39| active | boolean | Whether the **\<Navigator>** component is activated. If the component is activated, the corresponding navigation takes effect.|
40| params | object  | Data that needs to be passed to the target page during redirection. You can use [router.getParams()](../apis/js-apis-router.md#routergetparams) to obtain the data on the target page.|
41| target | string         | Path of the target page to be redirected to. The target page must be added to the **main_pages.json** file.                        |
42| type   | [NavigationType](#navigationtype)  | Navigation type.<br>Default value: **NavigationType.Push**|
43
44
45## Example
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)) // Pass parameters to the Detail page.
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  // Receive the input parameters of 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![en-us_image_0000001212058486](figures/en-us_image_0000001212058486.gif)
130