js-apis-system-router.md 11.7 KB
Newer Older
E
ester.zhou 已提交
1
# @system.router (Page Routing)
W
wusongqing 已提交
2

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
// Current page
export default {
  pushPage() {
    router.push({
      uri: 'pages/routerpage2/routerpage2',
      params: {
        data1: 'message',
        data2: {
          data3: [123, 456, 789]
E
ester.zhou 已提交
46 47
        }
      }
W
wusongqing 已提交
48 49 50 51 52 53
    });
  }
}
```


W
wusongqing 已提交
54
```js
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// routerpage2 page
export default {
  data: {
    data1: 'default',
    data2: {
      data3: [1, 2, 3]
    }
  },
  onInit() {
    console.info('showData1:' + this.data1);
    console.info('showData3:' + this.data2.data3);
  }
}
```

E
ester.zhou 已提交
70 71
> **NOTE**
>
W
wusongqing 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
> 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 已提交
85 86 87
| Name    | Type                             | Mandatory  | Description                        |
| ------- | ------------------------------- | ---- | -------------------------- |
| options | [RouterOptions](#routeroptions) | Yes   | Page routing parameters. For details, see **RouterOptions**.|
W
wusongqing 已提交
88 89 90

**Example**

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


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

**Example**

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


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


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


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


W
wusongqing 已提交
178
```js
W
wusongqing 已提交
179 180 181 182 183 184 185 186
// Return to the detail page through router.back().
export default {    
  backToDetail() {        
    router.back({uri:'pages/detail/detail'});    
  }
}
```

E
ester.zhou 已提交
187 188
> **NOTE**
>
W
wusongqing 已提交
189 190 191 192 193 194 195 196 197 198 199 200
> 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 已提交
201 202
| Type                                 | Description                   |
| ----------------------------------- | --------------------- |
W
wusongqing 已提交
203 204 205 206 207 208 209 210 211 212 213 214
| [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 已提交
215
```js
W
wusongqing 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
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 已提交
233 234
| Type    | Description                |
| ------ | ------------------ |
W
wusongqing 已提交
235 236 237 238
| string | Number of pages in the stack. The maximum value is **32**.|

**Example**

W
wusongqing 已提交
239
```js
W
wusongqing 已提交
240 241
export default {     
  getLength() {        
E
ester.zhou 已提交
242
    let size = router.getLength();        
W
wusongqing 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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 已提交
258 259
| Type                       | Description               |
| --------------------------- | ----------------- |
W
wusongqing 已提交
260 261 262 263
| [RouterState](#routerstate) | For details, see **RouterState**.|

**Example**

W
wusongqing 已提交
264
```js
W
wusongqing 已提交
265 266
export default {     
  getState() {        
E
ester.zhou 已提交
267
    let page = router.getState();
W
wusongqing 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    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 已提交
285 286
| Name    | Type                                      | Mandatory  | Description                                    |
| ------- | ---------------------------------------- | ---- | -------------------------------------- |
W
wusongqing 已提交
287
| options | [EnableAlertBeforeBackPageOptions](#enablealertbeforebackpageoptions6) | Yes   | For details, see **EnableAlertBeforeBackPageOptions**.|
W
wusongqing 已提交
288 289 290

**Example**

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

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

**Example**

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

## RouterOptions

Defines the page routing parameters.

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

E
ester.zhou 已提交
344 345 346 347
| Name  | Type| Mandatory| Description                                                        |
| ------ | -------- | ---- | ------------------------------------------------------------ |
| 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 已提交
348 349 350 351 352 353 354 355


## BackRouterOptions

Defines the parameters for routing back.

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

E
ester.zhou 已提交
356 357 358 359
| 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 target page during redirection.<br>**System capability**: SystemCapability.ArkUI.ArkUI.Lite|
W
wusongqing 已提交
360 361 362 363 364 365 366

## RouterState

Defines the page state.

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

W
wusongqing 已提交
367 368 369 370 371
| 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 已提交
372 373 374 375 376 377 378

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

Defines the **EnableAlertBeforeBackPage** parameters.

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

W
wusongqing 已提交
379 380 381 382 383
| 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 已提交
384
| complete | () => void               | No  | Called when the dialog box is closed.                          |
W
wusongqing 已提交
385 386 387

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

G
Gloria 已提交
388
Defines the **DisableAlertBeforeBackPage** parameters.
W
wusongqing 已提交
389 390 391

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

W
wusongqing 已提交
392 393 394 395
| 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 已提交
396
| complete | () => void               | No  | Called when the dialog box is closed.                          |
W
wusongqing 已提交
397 398 399

## ParamsInterface

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