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

Z
zengyawen 已提交
3

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

Z
zengyawen 已提交
8

E
ester.zhou 已提交
9
The **\<Navigator>** component provides redirection to the target page.
Z
zengyawen 已提交
10 11 12


## Required Permissions
Z
zengyawen 已提交
13 14 15

None

Z
zengyawen 已提交
16 17

## Child Components
Z
zengyawen 已提交
18 19 20 21

This component can contain child components.


Z
zengyawen 已提交
22 23 24
## APIs

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

Creates a navigator.

Z
zengyawen 已提交
28
- Parameters
E
ester.zhou 已提交
29
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
30
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
31 32
  | target | string | Yes | - | Path of the target page to be redirected to. |
  | type | NavigationType | No | NavigationType.Push | Navigation type. |
Z
zengyawen 已提交
33 34

- NavigationType enums
E
ester.zhou 已提交
35
  | Name | Description |
Z
zengyawen 已提交
36
  | -------- | -------- |
E
ester.zhou 已提交
37 38 39
  | 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 已提交
40

Z
zengyawen 已提交
41 42 43

## Attributes

E
ester.zhou 已提交
44
| Name | Parameters | Default Value | Description |
Z
zengyawen 已提交
45
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
46 47
| active | boolean | - | Whether the **\<Navigator>** component is activated. If the component is activated, the corresponding navigation takes effect. |
| params | Object | undefined | Data that needs to be passed to the target page during redirection. You can use **router.getParams()** to obtain the data on the target page. |
Z
zengyawen 已提交
48 49 50 51


## Example

E
ester.zhou 已提交
52 53 54

```ts
// Navigator.ets
Z
zengyawen 已提交
55 56 57 58
@Entry
@Component
struct NavigatorExample {
  @State active: boolean = false
E
ester.zhou 已提交
59
  @State Text: object = {name: 'news'}
Z
zengyawen 已提交
60 61 62 63

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

      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 已提交
79 80 81

```ts
// Detail.ets
Z
zengyawen 已提交
82 83 84 85 86
import router from '@system.router'

@Entry
@Component
struct DetailExample {
E
ester.zhou 已提交
87
  @State text: any = router.getParams().text
Z
zengyawen 已提交
88 89 90 91 92 93 94

  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 已提交
95 96
      Text('This is ' + this.text['name'] + ' page')
          .width('100%').textAlign(TextAlign.Center)
Z
zengyawen 已提交
97 98 99 100 101 102 103
    }
    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
  }
}

```

E
ester.zhou 已提交
104 105
```ts
// Back.ets
Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
@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 已提交
119
![en-us_image_0000001212058486](figures/en-us_image_0000001212058486.gif)