ts-container-navigator.md 3.7 KB
Newer Older
Z
zengyawen 已提交
1
# Navigator
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The **\<Navigator>** component provides redirection.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
Z
zengyawen 已提交
7
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Supported
Z
zengyawen 已提交
13 14


Z
zengyawen 已提交
15 16 17
## APIs

Navigator(value?: {target: string, type?: NavigationType})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22
**Parameters**

| Name| Type      | Mandatory| Description                                      |
| ------ | -------------- | ---- | ---------------------------------------------- |
E
ester.zhou 已提交
23 24
| target | string         | Yes  | Path of the target page to be redirected to.    |
| type   | [NavigationType](#navigationtype) | No  | Navigation type.<br>Default value: **NavigationType.Push**|
Z
zengyawen 已提交
25

E
ester.zhou 已提交
26
## NavigationType
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28 29 30 31 32
| Name     | Description                        |
| ------- | -------------------------- |
| Push    | Navigates to a specified page in the application.              |
| Replace | Replaces the current page with another one in the application and destroys the current page.|
| Back    | Returns to the previous page or a specified page.             |
Z
zengyawen 已提交
33

Z
zengyawen 已提交
34 35 36

## Attributes

E
ester.zhou 已提交
37 38 39
| Name  | Parameter   | Description                                                        |
| ------ | ------- | ------------------------------------------------------------ |
| active | boolean | Whether the **\<Navigator>** component is activated. If the component is activated, the corresponding navigation takes effect.|
E
ester.zhou 已提交
40 41 42
| 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.|
| target | string         | Path of the target page to be redirected to. The target page must be added to the **main_pages.json** file.                        |
| type   | [NavigationType](#navigationtype)  | Navigation type.<br>Default value: **NavigationType.Push**|
Z
zengyawen 已提交
43 44 45 46


## Example

E
ester.zhou 已提交
47 48
```ts
// Navigator.ets
Z
zengyawen 已提交
49 50 51 52
@Entry
@Component
struct NavigatorExample {
  @State active: boolean = false
E
ester.zhou 已提交
53
  @State Text: object = {name: 'news'}
Z
zengyawen 已提交
54 55 56 57

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Navigator({ target: 'pages/container/navigator/Detail', type: NavigationType.Push }) {
E
ester.zhou 已提交
58
        Text('Go to ' + this.Text['name'] + ' page')
E
ester.zhou 已提交
59 60
          .width('100%').textAlign(TextAlign.Center)
      }.params({ text: this.Text}) // Transfer parameters to the Detail page.
Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71 72

      Navigator() {
        Text('Back to previous page').width('100%').textAlign(TextAlign.Center)
      }.active(this.active)
      .onClick(() => {
        this.active = true
      })
    }.height(150).width(350).padding(35)
  }
}
```

E
ester.zhou 已提交
73 74
```ts
// Detail.ets
E
ester.zhou 已提交
75
import router from '@ohos.router'
Z
zengyawen 已提交
76 77 78 79

@Entry
@Component
struct DetailExample {
E
ester.zhou 已提交
80
  // Receive the input parameters of Navigator.ets.
E
ester.zhou 已提交
81
  @State text: any = router.getParams().text
Z
zengyawen 已提交
82 83 84 85 86 87 88

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Navigator({ target: 'pages/container/navigator/Back', type: NavigationType.Push }) {
        Text('Go to back page').width('100%').height(20)
      }

E
ester.zhou 已提交
89
      Text('This is ' + this.text['name'] + ' page')
E
ester.zhou 已提交
90
        .width('100%').textAlign(TextAlign.Center)
Z
zengyawen 已提交
91 92 93 94 95 96 97
    }
    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
  }
}

```

E
ester.zhou 已提交
98 99
```ts
// Back.ets
Z
zengyawen 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
@Entry
@Component
struct BackExample {
  build() {
    Column() {
      Navigator({ target: 'pages/container/navigator/Navigator', type: NavigationType.Back }) {
        Text('Return to Navigator Page').width('100%').textAlign(TextAlign.Center)
      }
    }.width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
113
![en-us_image_0000001212058486](figures/en-us_image_0000001212058486.gif)