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

W
wusongqing 已提交
3
> **NOTE**
W
wusongqing 已提交
4
>
W
wusongqing 已提交
5
> - 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 已提交
6 7 8
>
>
> - 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 已提交
9 10 11 12 13


## Modules to Import


W
wusongqing 已提交
14
```js
W
wusongqing 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
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 已提交
28 29 30
| Name    | Type                             | Mandatory  | Description                        |
| ------- | ------------------------------- | ---- | -------------------------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. For details, see **RouterOptions**.|
W
wusongqing 已提交
31 32 33

**Example**

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


W
wusongqing 已提交
52
```js
W
wusongqing 已提交
53 54 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
// 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 已提交
82 83 84
| Name    | Type                             | Mandatory  | Description                        |
| ------- | ------------------------------- | ---- | -------------------------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. For details, see **RouterOptions**.|
W
wusongqing 已提交
85 86 87

**Example**

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


W
wusongqing 已提交
103
```js
W
wusongqing 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// 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 已提交
125 126 127
| Name    | Type                                     | Mandatory  | Description                     |
| ------- | --------------------------------------- | ---- | ----------------------- |
| options | [BackRouterOptions](#backrouteroptions) | Yes   | For details, see **BackRouterOptions**.|
W
wusongqing 已提交
128 129 130

**Example**

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


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


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


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


W
wusongqing 已提交
175
```js
W
wusongqing 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
// 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 已提交
197 198
| Type                                 | Description                   |
| ----------------------------------- | --------------------- |
W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210
| [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 已提交
211
```js
W
wusongqing 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
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 已提交
229 230
| Type    | Description                |
| ------ | ------------------ |
W
wusongqing 已提交
231 232 233 234
| string | Number of pages in the stack. The maximum value is **32**.|

**Example**

W
wusongqing 已提交
235
```js
W
wusongqing 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
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 已提交
254 255
| Type                       | Description               |
| --------------------------- | ----------------- |
W
wusongqing 已提交
256 257 258 259
| [RouterState](#routerstate) | For details, see **RouterState**.|

**Example**

W
wusongqing 已提交
260
```js
W
wusongqing 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
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 已提交
281 282
| Name    | Type                                      | Mandatory  | Description                                    |
| ------- | ---------------------------------------- | ---- | -------------------------------------- |
W
wusongqing 已提交
283
| options | [EnableAlertBeforeBackPageOptions](#enablealertbeforebackpageoptions6) | Yes   | For details, see **EnableAlertBeforeBackPageOptions**.|
W
wusongqing 已提交
284 285 286

**Example**

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

## 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 已提交
313 314 315
| Name    | Type                                      | Mandatory  | Description                                     |
| ------- | ---------------------------------------- | ---- | --------------------------------------- |
| options | [DisableAlertBeforeBackPageOptions](#disablealertbeforebackpageoptions6) | No   | For details, see **DisableAlertBeforeBackPageOptions**.|
W
wusongqing 已提交
316 317 318

**Example**

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

## RouterOptions

Defines the page routing parameters.

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

W
wusongqing 已提交
340 341 342 343
| Name    | Type  | Mandatory  | Description                                      |
| ------ | ------ | ---- | ---------------------------------------- |
| uri    | string | Yes   | URI of the destination 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 destination page during redirection. After the destination page is displayed, it can use the passed data, for example, **this.data1** (**data1** is a key in **params**). If there is the same key (for example, **data1**) on the destination page, the passed **data1** value will replace the original value on the destination page.|
W
wusongqing 已提交
344 345 346 347 348 349 350 351


## 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 已提交
352 353 354 355
| 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|
| params | Object | No   | Data that needs to be passed to the destination page during redirection.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Lite|
W
wusongqing 已提交
356 357 358 359 360 361 362

## RouterState

Defines the page state.

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

W
wusongqing 已提交
363 364 365 366 367
| 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 已提交
368 369 370 371 372 373 374

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

Defines the **EnableAlertBeforeBackPage** parameters.

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

W
wusongqing 已提交
375 376 377 378 379 380
| Name      | Type                    | Mandatory  | Description                       |
| -------- | ------------------------ | ---- | ------------------------- |
| message  | string                   | Yes   | Content displayed in the confirm dialog box.                 |
| success  | (errMsg: string) => void | No   | Called when a dialog box is displayed. **errMsg** indicates the returned information.   |
| fail     | (errMsg: string) => void | No   | Called when the API fails to be called. **errMsg** indicates the returned information.|
| complete | () => void               | No   | Called when the API call is complete.             |
W
wusongqing 已提交
381 382 383 384 385 386 387

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

Define the **DisableAlertBeforeBackPage** parameters.

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

W
wusongqing 已提交
388 389 390 391 392
| Name      | Type                    | Mandatory  | Description                       |
| -------- | ------------------------ | ---- | ------------------------- |
| success  | (errMsg: string) => void | No   | Called when a dialog box is displayed. **errMsg** indicates the returned information.   |
| fail     | (errMsg: string) => void | No   | Called when the API fails to be called. **errMsg** indicates the returned information.|
| complete | () => void               | No   | Called when the API call is complete.             |
W
wusongqing 已提交
393 394 395

## ParamsInterface

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