ts-container-navigator.md 3.5 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 23 24
**Parameters**

| Name| Type      | Mandatory| Description                                      |
| ------ | -------------- | ---- | ---------------------------------------------- |
| target | string         | Yes  | Path of the target page to be redirected to.                      |
| type   | 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 40 41 42
| Name  | Parameter   | Description                                                        |
| ------ | ------- | ------------------------------------------------------------ |
| active | boolean | Whether the **\<Navigator>** component is activated. If the component is activated, the corresponding navigation takes effect.|
| params | object  | 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.|
| target | string         | Path of the target page to be redirected to.                      |
| type   | NavigationType | Navigation mode.<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 59
        Text('Go to ' + this.Text['name'] + ' page')
            .width('100%').textAlign(TextAlign.Center)
Z
zengyawen 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
      }.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 已提交
73 74
```ts
// Detail.ets
Z
zengyawen 已提交
75 76 77 78 79
import router from '@system.router'

@Entry
@Component
struct DetailExample {
E
ester.zhou 已提交
80
  @State text: any = router.getParams().text
Z
zengyawen 已提交
81 82 83 84 85 86 87

  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 已提交
88 89
      Text('This is ' + this.text['name'] + ' page')
          .width('100%').textAlign(TextAlign.Center)
Z
zengyawen 已提交
90 91 92 93 94 95 96
    }
    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
  }
}

```

E
ester.zhou 已提交
97 98
```ts
// Back.ets
Z
zengyawen 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
@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 已提交
112
![en-us_image_0000001212058486](figures/en-us_image_0000001212058486.gif)