• 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     | Description                        |
29| ------- | -------------------------- |
30| Push    | Navigates to the specified page in the application.              |
31| Replace | Replaces the current page with another one in the application and destroys the current page.|
32| Back    | 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 Text: object = {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.Text['name'] + ' page')
59          .width('100%').textAlign(TextAlign.Center)
60      }.params({ text: this.Text}) // Transfer 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```
72
73```ts
74// Detail.ets
75import router from '@ohos.router'
76
77@Entry
78@Component
79struct DetailExample {
80  // Receive the input parameters of Navigator.ets.
81  @State text: any = router.getParams()['text']
82
83  build() {
84    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
85      Navigator({ target: 'pages/container/navigator/Back', type: NavigationType.Push }) {
86        Text('Go to back page').width('100%').height(20)
87      }
88
89      Text('This is ' + this.text['name'] + ' page')
90        .width('100%').textAlign(TextAlign.Center)
91    }
92    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
93  }
94}
95
96```
97
98```ts
99// Back.ets
100@Entry
101@Component
102struct BackExample {
103  build() {
104    Column() {
105      Navigator({ target: 'pages/container/navigator/Navigator', type: NavigationType.Back }) {
106        Text('Return to Navigator Page').width('100%').textAlign(TextAlign.Center)
107      }
108    }.width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
109  }
110}
111```
112
113![en-us_image_0000001212058486](figures/en-us_image_0000001212058486.gif)
114