js-apis-system-router.md 11.7 KB
Newer Older
W
wusongqing 已提交
1 2
# Page Routing

W
wusongqing 已提交
3 4
The **Router** module provides APIs to access pages through URIs.

W
wusongqing 已提交
5
> **NOTE**
W
wusongqing 已提交
6
>
W
wusongqing 已提交
7
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.router`](js-apis-router.md) instead.
W
wusongqing 已提交
8 9 10
>
>
> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
W
wusongqing 已提交
11 12 13 14 15


## Modules to Import


W
wusongqing 已提交
16
```js
W
wusongqing 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
import router from '@system.router';
```

## router.push

push(options: RouterOptions): void

Navigates to a specified page in the application.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

W
wusongqing 已提交
30 31 32
| Name    | Type                             | Mandatory  | Description                        |
| ------- | ------------------------------- | ---- | -------------------------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. For details, see **RouterOptions**.|
W
wusongqing 已提交
33 34 35

**Example**

W
wusongqing 已提交
36
```js
W
wusongqing 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// Current page
export default {
  pushPage() {
    router.push({
      uri: 'pages/routerpage2/routerpage2',
      params: {
        data1: 'message',
        data2: {
          data3: [123, 456, 789]
	},
      },
    });
  }
}
```


W
wusongqing 已提交
54
```js
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// routerpage2 page
export default {
  data: {
    data1: 'default',
    data2: {
      data3: [1, 2, 3]
    }
  },
  onInit() {
    console.info('showData1:' + this.data1);
    console.info('showData3:' + this.data2.data3);
  }
}
```

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The page routing stack supports a maximum of 32 pages.


## router.replace

replace(options: RouterOptions): void

Replaces the current page with another one in the application and destroys the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Lite

**Parameters**

W
wusongqing 已提交
84 85 86
| Name    | Type                             | Mandatory  | Description                        |
| ------- | ------------------------------- | ---- | -------------------------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. For details, see **RouterOptions**.|
W
wusongqing 已提交
87 88 89

**Example**

W
wusongqing 已提交
90
```js
W
wusongqing 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
// Current page
export default {
  replacePage() {
    router.replace({
      uri: 'pages/detail/detail',
      params: {
        data1: 'message',
      },
    });
  }
}
```


W
wusongqing 已提交
105
```js
W
wusongqing 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
// detail page
export default {
  data: {
    data1: 'default'
  },
  onInit() {
    console.info('showData1:' + this.data1)
  }
}
```

## router.back

back(options?: BackRouterOptions): void

Returns to the previous page or a specified page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

W
wusongqing 已提交
127 128 129
| Name    | Type                                     | Mandatory  | Description                     |
| ------- | --------------------------------------- | ---- | ----------------------- |
| options | [BackRouterOptions](#backrouteroptions) | Yes   | For details, see **BackRouterOptions**.|
W
wusongqing 已提交
130 131 132

**Example**

W
wusongqing 已提交
133
```js
W
wusongqing 已提交
134 135 136 137 138 139 140 141 142 143 144
// index page
export default {    
  indexPushPage() {        
    router.push({            
      uri: 'pages/detail/detail',        
    });        
  }
}
```


W
wusongqing 已提交
145
```js
W
wusongqing 已提交
146 147 148 149 150 151 152 153 154 155 156
// detail page
export default {    
  detailPushPage() {        
    router.push({            
      uri: 'pages/mall/mall',        
    });    
  }
}
```


W
wusongqing 已提交
157
```js
W
wusongqing 已提交
158 159 160 161 162 163 164 165 166
// Navigate from the mall page to the detail page through router.back().
export default {    
  mallBackPage() {        
    router.back();    
  }
}
```


W
wusongqing 已提交
167
```js
W
wusongqing 已提交
168 169 170 171 172 173 174 175 176
// Navigate from the detail page to the index page through router.back().
export default {    
  defaultBack() {        
    router.back();    
  }
}
```


W
wusongqing 已提交
177
```js
W
wusongqing 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
// Return to the detail page through router.back().
export default {    
  backToDetail() {        
    router.back({uri:'pages/detail/detail'});    
  }
}
```

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> In the example, the **uri** field indicates the page route, which is specified by the **pages** list in the **config.json** file.

## router.getParams

getParams(): ParamsInterface

Obtains parameter information about the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Return value**

W
wusongqing 已提交
199 200
| Type                                 | Description                   |
| ----------------------------------- | --------------------- |
W
wusongqing 已提交
201 202 203 204 205 206 207 208 209 210 211 212
| [ParamsInterface](#paramsinterface) | For details, see **ParamsInterface**.|

## router.clear

clear(): void

Clears all historical pages in the stack and retains only the current page at the top of the stack.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Example**

W
wusongqing 已提交
213
```js
W
wusongqing 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
export default {    
  clearPage() {        
    router.clear();    
  }
}
```

## router.getLength

getLength(): string

Obtains the number of pages in the current stack.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Return value**

W
wusongqing 已提交
231 232
| Type    | Description                |
| ------ | ------------------ |
W
wusongqing 已提交
233 234 235 236
| string | Number of pages in the stack. The maximum value is **32**.|

**Example**

W
wusongqing 已提交
237
```js
W
wusongqing 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
export default {     
  getLength() {        
    var size = router.getLength();        
    console.log('pages stack size = ' + size);    
  }
}
```

## router.getState

getState(): RouterState

Obtains state information about the current page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Return value**

W
wusongqing 已提交
256 257
| Type                       | Description               |
| --------------------------- | ----------------- |
W
wusongqing 已提交
258 259 260 261
| [RouterState](#routerstate) | For details, see **RouterState**.|

**Example**

W
wusongqing 已提交
262
```js
W
wusongqing 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
export default {     
  getState() {        
    var page = router.getState();
    console.log('current index = ' + page.index);
    console.log('current name = ' + page.name);
    console.log('current path = ' + page.path);
  }
}
```

## router.enableAlertBeforeBackPage<sup>6+</sup>

enableAlertBeforeBackPage(options: EnableAlertBeforeBackPageOptions): void

Enables the display of a confirm dialog box before returning to the previous page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

W
wusongqing 已提交
283 284
| Name    | Type                                      | Mandatory  | Description                                    |
| ------- | ---------------------------------------- | ---- | -------------------------------------- |
W
wusongqing 已提交
285
| options | [EnableAlertBeforeBackPageOptions](#enablealertbeforebackpageoptions6) | Yes   | For details, see **EnableAlertBeforeBackPageOptions**.|
W
wusongqing 已提交
286 287 288

**Example**

W
wusongqing 已提交
289
```js
W
wusongqing 已提交
290 291 292 293 294 295 296
export default {    
  enableAlertBeforeBackPage() {        
    router.enableAlertBeforeBackPage({            
      message: 'Message Info',            
      success: function() {                
        console.log('success');            
      },            
W
wusongqing 已提交
297 298
      cancel: function() {                
        console.log('cancel');            
W
wusongqing 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
      },        
    });    
  }
}
```

## router.disableAlertBeforeBackPage<sup>6+</sup>

disableAlertBeforeBackPage(options?: DisableAlertBeforeBackPageOptions): void

Disables the display of a confirm dialog box before returning to the previous page.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

**Parameters**

W
wusongqing 已提交
315 316 317
| Name    | Type                                      | Mandatory  | Description                                     |
| ------- | ---------------------------------------- | ---- | --------------------------------------- |
| options | [DisableAlertBeforeBackPageOptions](#disablealertbeforebackpageoptions6) | No   | For details, see **DisableAlertBeforeBackPageOptions**.|
W
wusongqing 已提交
318 319 320

**Example**

W
wusongqing 已提交
321
```js
W
wusongqing 已提交
322 323 324 325 326 327
export default {    
  disableAlertBeforeBackPage() {        
    router.disableAlertBeforeBackPage({            
      success: function() {                
        console.log('success');            
      },            
W
wusongqing 已提交
328 329
      cancel: function() {                
        console.log('cancel');            
W
wusongqing 已提交
330 331 332 333 334 335 336 337 338 339 340 341
      },        
    });    
  }
}
```

## RouterOptions

Defines the page routing parameters.

**System capability**: SystemCapability.ArkUI.ArkUI.Lite

W
wusongqing 已提交
342 343
| Name    | Type  | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
G
Gloria 已提交
344 345
| uri    | string | Yes   | URI of the target page, in either of the following formats:<br>1. Absolute path, which is provided by the **pages** list in the **config.json** file. Example:<br>- pages/index/index<br> - pages/detail/detail<br>2. Specific path. If the URI is a slash (/), the home page is displayed.|
| params | Object | No   | Data that needs to be passed to the target page during redirection. The target page can use **router.getParams()** to obtain the passed parameters, for example, **this.keyValue** (**keyValue** is the value of a key in **params**). In the web-like paradigm, these parameters can be directly used on the target page. If the field specified by **key** already exists on the target page, the passed value of the key will be displayed.|
W
wusongqing 已提交
346 347 348 349 350 351 352 353


## BackRouterOptions

Defines the parameters for routing back.

**System capability**: The items in the table below require different system capabilities. For details, see the table.

W
wusongqing 已提交
354 355 356
| Name    | Type  | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| uri    | string | No   | URI of the page to return to. If the specified page does not exist in the page stack, the application does not respond. If this parameter is not set, the application returns to the previous page.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Full|
G
Gloria 已提交
357
| params | Object | No   | Data that needs to be passed to the target page during redirection.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Lite|
W
wusongqing 已提交
358 359 360 361 362 363 364

## RouterState

Defines the page state.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
365 366 367 368 369
| Name   | Type  | Mandatory  | Description                                |
| ----- | ------ | ---- | ---------------------------------- |
| index | number | Yes   | Index of the current page in the stack. The index starts from 1 from the bottom to the top of the stack.|
| name  | string | Yes   | Name of the current page, that is, the file name.                 |
| path  | string | Yes   | Path of the current page.                        |
W
wusongqing 已提交
370 371 372 373 374 375 376

## EnableAlertBeforeBackPageOptions<sup>6+</sup>

Defines the **EnableAlertBeforeBackPage** parameters.

**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
377 378 379 380 381
| Name    | Type                | Mandatory| Description                                              |
| -------- | ------------------------ | ---- | -------------------------------------------------- |
| message  | string                   | Yes  | Content displayed in the confirm dialog box.                                  |
| success  | (errMsg: string) => void | No  | Called when the **OK** button in the confirm dialog box is clicked. **errMsg** indicates the returned information.|
| cancel   | (errMsg: string) => void | No  | Called when the **Cancel** button in the confirm dialog box is clicked. **errMsg** indicates the returned information.|
G
Gloria 已提交
382
| complete | () => void               | No  | Called when the dialog box is closed.                          |
W
wusongqing 已提交
383 384 385

## DisableAlertBeforeBackPageOptions<sup>6+</sup>

G
Gloria 已提交
386
Defines the **DisableAlertBeforeBackPage** parameters.
W
wusongqing 已提交
387 388 389

**System capability**: SystemCapability.ArkUI.ArkUI.Full

W
wusongqing 已提交
390 391 392 393
| Name    | Type                | Mandatory| Description                                              |
| -------- | ------------------------ | ---- | -------------------------------------------------- |
| success  | (errMsg: string) => void | No  | Called when the dialog box is closed. **errMsg** indicates the returned information.|
| cancel   | (errMsg: string) => void | No  | Called when the dialog box fails to be closed. **errMsg** indicates the returned information.|
G
Gloria 已提交
394
| complete | () => void               | No  | Called when the dialog box is closed.                          |
W
wusongqing 已提交
395 396 397

## ParamsInterface

W
wusongqing 已提交
398 399 400
| Name           | Type  | Description     |
| ------------- | ------ | ------- |
| [key: string] | Object | List of routing parameters.|