ts-container-navigator.md 6.8 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4
# Navigator<a name="ZH-CN_TOPIC_0000001192915106"></a>

>![](../../public_sys-resources/icon-note.gif) **说明:** 
>该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

路由容器组件,提供路由跳转能力。

## 权限列表<a name="section1933872716571"></a>



## 子组件<a name="section1146935319579"></a>

可以包含子组件。

## 接口<a name="section5861121019581"></a>

Navigator\(value?: \{target: string, type?: NavigationType\}\)

创建路由组件。

-   参数

Z
zengyawen 已提交
24
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="16.11%" id="mcps1.1.6.1.1"><p>参数名</p>
Z
zengyawen 已提交
25
    </th>
Z
zengyawen 已提交
26
    <th class="cellrowborder" valign="top" width="18%" id="mcps1.1.6.1.2"><p>参数类型</p>
Z
zengyawen 已提交
27
    </th>
Z
zengyawen 已提交
28
    <th class="cellrowborder" valign="top" width="8.25%" id="mcps1.1.6.1.3"><p>必填</p>
Z
zengyawen 已提交
29
    </th>
Z
zengyawen 已提交
30
    <th class="cellrowborder" valign="top" width="21.42%" id="mcps1.1.6.1.4"><p>默认值</p>
Z
zengyawen 已提交
31
    </th>
Z
zengyawen 已提交
32
    <th class="cellrowborder" valign="top" width="36.22%" id="mcps1.1.6.1.5"><p>参数描述</p>
Z
zengyawen 已提交
33 34 35
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
36
    <tbody><tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.1.6.1.1 "><p>target</p>
Z
zengyawen 已提交
37
    </td>
Z
zengyawen 已提交
38
    <td class="cellrowborder" valign="top" width="18%" headers="mcps1.1.6.1.2 "><p>string</p>
Z
zengyawen 已提交
39
    </td>
Z
zengyawen 已提交
40
    <td class="cellrowborder" valign="top" width="8.25%" headers="mcps1.1.6.1.3 "><p>是</p>
Z
zengyawen 已提交
41
    </td>
Z
zengyawen 已提交
42
    <td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.1.6.1.4 "><p>-</p>
Z
zengyawen 已提交
43
    </td>
Z
zengyawen 已提交
44
    <td class="cellrowborder" valign="top" width="36.22%" headers="mcps1.1.6.1.5 "><p>指定跳转目标页面的路径。</p>
Z
zengyawen 已提交
45 46
    </td>
    </tr>
Z
zengyawen 已提交
47
    <tr><td class="cellrowborder" valign="top" width="16.11%" headers="mcps1.1.6.1.1 "><p>type</p>
Z
zengyawen 已提交
48
    </td>
Z
zengyawen 已提交
49
    <td class="cellrowborder" valign="top" width="18%" headers="mcps1.1.6.1.2 "><p><a href="#table3452114216394">NavigationType</a></p>
Z
zengyawen 已提交
50
    </td>
Z
zengyawen 已提交
51
    <td class="cellrowborder" valign="top" width="8.25%" headers="mcps1.1.6.1.3 "><p>否</p>
Z
zengyawen 已提交
52
    </td>
Z
zengyawen 已提交
53
    <td class="cellrowborder" valign="top" width="21.42%" headers="mcps1.1.6.1.4 "><p>Push</p>
Z
zengyawen 已提交
54
    </td>
Z
zengyawen 已提交
55
    <td class="cellrowborder" valign="top" width="36.22%" headers="mcps1.1.6.1.5 "><p>指定路由方式。</p>
Z
zengyawen 已提交
56 57 58 59 60 61 62 63
    </td>
    </tr>
    </tbody>
    </table>

-   NavigationType枚举说明

    <a name="table3452114216394"></a>
Z
zengyawen 已提交
64
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="25.2%" id="mcps1.1.3.1.1"><p>名称</p>
Z
zengyawen 已提交
65
    </th>
Z
zengyawen 已提交
66
    <th class="cellrowborder" valign="top" width="74.8%" id="mcps1.1.3.1.2"><p>描述</p>
Z
zengyawen 已提交
67 68 69
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
70
    <tbody><tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Push</p>
Z
zengyawen 已提交
71
    </td>
Z
zengyawen 已提交
72
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>跳转到应用内的指定页面。</p>
Z
zengyawen 已提交
73 74
    </td>
    </tr>
Z
zengyawen 已提交
75
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Replace</p>
Z
zengyawen 已提交
76
    </td>
Z
zengyawen 已提交
77
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>用应用内的某个页面替换当前页面,并销毁被替换的页面。</p>
Z
zengyawen 已提交
78 79
    </td>
    </tr>
Z
zengyawen 已提交
80
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Back</p>
Z
zengyawen 已提交
81
    </td>
Z
zengyawen 已提交
82
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>返回上一页面或指定的页面。</p>
Z
zengyawen 已提交
83 84 85 86 87 88 89 90
    </td>
    </tr>
    </tbody>
    </table>


## 属性<a name="section187781816826"></a>

Z
zengyawen 已提交
91
<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="11.871187118711871%" id="mcps1.1.5.1.1"><p>名称</p>
Z
zengyawen 已提交
92
</th>
Z
zengyawen 已提交
93
<th class="cellrowborder" valign="top" width="16.291629162916294%" id="mcps1.1.5.1.2"><p>参数</p>
Z
zengyawen 已提交
94
</th>
Z
zengyawen 已提交
95
<th class="cellrowborder" valign="top" width="12.73127312731273%" id="mcps1.1.5.1.3"><p>默认值</p>
Z
zengyawen 已提交
96
</th>
Z
zengyawen 已提交
97
<th class="cellrowborder" valign="top" width="59.10591059105911%" id="mcps1.1.5.1.4"><p>描述</p>
Z
zengyawen 已提交
98 99 100
</th>
</tr>
</thead>
Z
zengyawen 已提交
101
<tbody><tr><td class="cellrowborder" valign="top" width="11.871187118711871%" headers="mcps1.1.5.1.1 "><p>active</p>
Z
zengyawen 已提交
102
</td>
Z
zengyawen 已提交
103
<td class="cellrowborder" valign="top" width="16.291629162916294%" headers="mcps1.1.5.1.2 "><p>boolean</p>
Z
zengyawen 已提交
104
</td>
Z
zengyawen 已提交
105
<td class="cellrowborder" valign="top" width="12.73127312731273%" headers="mcps1.1.5.1.3 "><p>-</p>
Z
zengyawen 已提交
106
</td>
Z
zengyawen 已提交
107
<td class="cellrowborder" valign="top" width="59.10591059105911%" headers="mcps1.1.5.1.4 "><p>当前路由组件是否处于激活状态,处于激活状态时,会生效相应的路由操作。</p>
Z
zengyawen 已提交
108 109
</td>
</tr>
Z
zengyawen 已提交
110
<tr><td class="cellrowborder" valign="top" width="11.871187118711871%" headers="mcps1.1.5.1.1 "><p>params</p>
Z
zengyawen 已提交
111
</td>
Z
zengyawen 已提交
112
<td class="cellrowborder" valign="top" width="16.291629162916294%" headers="mcps1.1.5.1.2 "><p>Object</p>
Z
zengyawen 已提交
113
</td>
Z
zengyawen 已提交
114
<td class="cellrowborder" valign="top" width="12.73127312731273%" headers="mcps1.1.5.1.3 "><p>undefined</p>
Z
zengyawen 已提交
115
</td>
Z
zengyawen 已提交
116
<td class="cellrowborder" valign="top" width="59.10591059105911%" headers="mcps1.1.5.1.4 "><p>跳转时要同时传递到目标页面的数据,可在目标页面使用router.getParams()获得。</p>
Z
zengyawen 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
</td>
</tr>
</tbody>
</table>

## 示例<a name="section186436131831"></a>

```
// Navigator Page
@Entry
@Component
struct NavigatorExample {
  @State active: boolean = false
  @State Text: string = 'news'

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Navigator({ target: 'pages/container/navigator/Detail', type: NavigationType.Push }) {
        Text('Go to ' + this.Text + ' page').width('100%').textAlign(TextAlign.Center)
      }.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)
  }
}
```

```
// Detail Page
import router from '@system.router'

@Entry
@Component
struct DetailExample {
  @State text: string = router.getParams().text

  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)
      }

      Text('This is ' + this.text + ' page').width('100%').textAlign(TextAlign.Center)
    }
    .width('100%').height(200).padding({ left: 35, right: 35, top: 35 })
  }
}

```

```
// Back Page
@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 })
  }
}
```

![](figures/Navigator.gif)