1e41f4b71Sopenharmony_ci# Page Routing (@ohos.router) (Not Recommended) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciPage routing refers to the redirection and data transfer between different pages in an application. It can be implemented through APIs of the **Router** module. Through different URLs, you can easily navigate users through pages. This document describes the functions provided by the **Router** module from the following aspects: [Page Redirection](#page-redirection), [Page Return](#page-return), [Adding a Confirmation Dialog Box Before Page Return](#adding-a-confirmation-dialog-box-before-page-return), and [Named Route](#named-route). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci>**NOTE** 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci>You are advised to use [Component Navigation (Navigation)](./arkts-navigation-navigation.md), which offers enhanced functionality and customization capabilities, as the routing framework in your application. For details about the differences between **Navigation** and **Router**, see [Transition from Router to Navigation](./arkts-router-to-navigation.md). 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Page Redirection 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciPage redirection is an important part of the development process. When using an application, you usually need to jump between different pages, and sometimes you need to pass data from one page to another. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci **Figure 1** Page redirection 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciThe **Router** module provides two redirection modes: [router.pushUrl](../reference/apis-arkui/js-apis-router.md#routerpushurl9) and [router.replaceUrl](../reference/apis-arkui/js-apis-router.md#routerreplaceurl9). Whether the target page will replace the current page depends on the mode used. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- **router.pushUrl**: The target page is pushed into the page stack and does not replace the current page. In this mode, the state of the current page is retained, and users can return to the current page by pressing the back button or calling the [router.back](../reference/apis-arkui/js-apis-router.md#routerback) API. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- **router.replaceUrl**: The target page replaces and destroys the current page. In this mode, the resources of the current page can be released, and users cannot return to the current page. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci>**NOTE** 24e41f4b71Sopenharmony_ci> 25e41f4b71Sopenharmony_ci>- When creating a page, configure the route to this page by following instructions in <!--RP1-->[Building the Second Page](../quick-start/start-with-ets-stage.md#building-the-second-page)<!--RP1End-->. 26e41f4b71Sopenharmony_ci> 27e41f4b71Sopenharmony_ci> 28e41f4b71Sopenharmony_ci>- The maximum capacity of a page stack is 32 pages. If this limit is exceeded, the [router.clear](../reference/apis-arkui/js-apis-router.md#routerclear) API can be called to clear the historical page stack and free the memory. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciThe **Router** module also provides two instance modes: **Standard** and **Single**. Depending on the mode, the target URL is mapped to one or more instances. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- **Standard**: multi-instance mode. It is the default instance mode. In this mode, the target page is added to the top of the page stack, regardless of whether a page with the same URL exists in the stack. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci- **Single**: singleton mode. In this mode, if the URL of the target page already exists in the page stack, the page closest to the top of the stack with the same URL is moved to the top of the stack and becomes the new page. If the URL of the target page does not exist in the page stack, the page is redirected in standard mode. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ciBefore using the **Router** module, import it first. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```ts 40e41f4b71Sopenharmony_ciimport { promptAction, router } from '@kit.ArkUI'; 41e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 42e41f4b71Sopenharmony_ci``` 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci- Scenario 1: There is a home page (**Home**) and a details page (**Detail**). You want to click an offering on the home page to go to the details page. In addition, the home page needs to be retained in the page stack so that the status can be restored when the page is returned. In this scenario, you can use the **pushUrl** API and use the **Standard** instance mode (which can also be omitted). 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```ts 48e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 49e41f4b71Sopenharmony_ci // On the Home page 50e41f4b71Sopenharmony_ci function onJumpClick(): void { 51e41f4b71Sopenharmony_ci router.pushUrl({ 52e41f4b71Sopenharmony_ci url: 'pages/Detail' // Target URL. 53e41f4b71Sopenharmony_ci }, router.RouterMode.Standard, (err) => { 54e41f4b71Sopenharmony_ci if (err) { 55e41f4b71Sopenharmony_ci console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`); 56e41f4b71Sopenharmony_ci return; 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci console.info('Invoke pushUrl succeeded.'); 59e41f4b71Sopenharmony_ci }); 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci >**NOTE** 64e41f4b71Sopenharmony_ci > 65e41f4b71Sopenharmony_ci >In standard (multi-instance) mode, the **router.RouterMode.Standard** parameter can be omitted. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci- Scenario 2: There is a login page (**Login**) and a personal center page (**Profile**). After a user successfully logs in from the **Login** page, the **Profile** page is displayed. At the same time, the **Login** page is destroyed, and the application exits when the back button is pressed. In this scenario, you can use the **replaceUrl** API and use the Standard instance mode (which can also be omitted). 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci ```ts 71e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 72e41f4b71Sopenharmony_ci // On the Login page 73e41f4b71Sopenharmony_ci function onJumpClick(): void { 74e41f4b71Sopenharmony_ci router.replaceUrl({ 75e41f4b71Sopenharmony_ci url: 'pages/Profile' // Target URL. 76e41f4b71Sopenharmony_ci }, router.RouterMode.Standard, (err) => { 77e41f4b71Sopenharmony_ci if (err) { 78e41f4b71Sopenharmony_ci console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`); 79e41f4b71Sopenharmony_ci return; 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci console.info('Invoke replaceUrl succeeded.'); 82e41f4b71Sopenharmony_ci }) 83e41f4b71Sopenharmony_ci } 84e41f4b71Sopenharmony_ci ``` 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci >**NOTE** 87e41f4b71Sopenharmony_ci > 88e41f4b71Sopenharmony_ci >In standard (multi-instance) mode, the **router.RouterMode.Standard** parameter can be omitted. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci- Scenario 3: There is a **Setting** page and a **Theme** page. After a theme option on the **Setting** page is clicked, the **Theme** page is displayed. Only one **Theme** page exists in the page stack at the same time. When the back button is clicked on the **Theme** page, the **Setting** page is displayed. In this scenario, you can use the **pushUrl** API and use the **Single** instance mode. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci ```ts 94e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 95e41f4b71Sopenharmony_ci // On the Setting page 96e41f4b71Sopenharmony_ci function onJumpClick(): void { 97e41f4b71Sopenharmony_ci router.pushUrl({ 98e41f4b71Sopenharmony_ci url: 'pages/Theme' // Target URL. 99e41f4b71Sopenharmony_ci }, router.RouterMode.Single, (err) => { 100e41f4b71Sopenharmony_ci if (err) { 101e41f4b71Sopenharmony_ci console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`); 102e41f4b71Sopenharmony_ci return; 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci console.info('Invoke pushUrl succeeded.'); 105e41f4b71Sopenharmony_ci }); 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci ``` 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci- Scenario 4: There is a search result list page (**SearchResult**) and a search result details page (**SearchDetail**). You want to click a result on the **SearchResult** page to go to the **SearchDetail** page. In addition, if the result has been viewed before, clicking the result displays the existing details page, instead of creating a new one. In this scenario, you can use the **replaceUrl** API and use the **Single** instance mode. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci ```ts 113e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci // On the SearchResult page 116e41f4b71Sopenharmony_ci function onJumpClick(): void { 117e41f4b71Sopenharmony_ci router.replaceUrl({ 118e41f4b71Sopenharmony_ci url: 'pages/SearchDetail' // Target URL. 119e41f4b71Sopenharmony_ci }, router.RouterMode.Single, (err) => { 120e41f4b71Sopenharmony_ci if (err) { 121e41f4b71Sopenharmony_ci console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`); 122e41f4b71Sopenharmony_ci return; 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci console.info('Invoke replaceUrl succeeded.'); 125e41f4b71Sopenharmony_ci }) 126e41f4b71Sopenharmony_ci } 127e41f4b71Sopenharmony_ci ``` 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciThe preceding scenarios do not involve parameter transfer. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciIf you need to transfer data to the target page during redirection, you can add a **params** attribute and specify an object as a parameter when invoking an API of the **Router** module. Example: 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci```ts 135e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 136e41f4b71Sopenharmony_ciclass DataModelInfo { 137e41f4b71Sopenharmony_ci age: number = 0; 138e41f4b71Sopenharmony_ci} 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ciclass DataModel { 141e41f4b71Sopenharmony_ci id: number = 0; 142e41f4b71Sopenharmony_ci info: DataModelInfo|null = null; 143e41f4b71Sopenharmony_ci} 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_cifunction onJumpClick(): void { 146e41f4b71Sopenharmony_ci // On the Home page 147e41f4b71Sopenharmony_ci let paramsInfo: DataModel = { 148e41f4b71Sopenharmony_ci id: 123, 149e41f4b71Sopenharmony_ci info: { 150e41f4b71Sopenharmony_ci age: 20 151e41f4b71Sopenharmony_ci } 152e41f4b71Sopenharmony_ci }; 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci router.pushUrl({ 155e41f4b71Sopenharmony_ci url: 'pages/Detail', // Target URL. 156e41f4b71Sopenharmony_ci params: paramsInfo // Add the params attribute to transfer custom parameters. 157e41f4b71Sopenharmony_ci }, (err) => { 158e41f4b71Sopenharmony_ci if (err) { 159e41f4b71Sopenharmony_ci console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`); 160e41f4b71Sopenharmony_ci return; 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci console.info('Invoke pushUrl succeeded.'); 163e41f4b71Sopenharmony_ci }) 164e41f4b71Sopenharmony_ci} 165e41f4b71Sopenharmony_ci``` 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ciOn the target page, you can call the [getParams](../reference/apis-arkui/js-apis-router.md#routergetparams) API of the **Router** module to obtain the passed parameters. Example: 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci```ts 171e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciclass InfoTmp { 174e41f4b71Sopenharmony_ci age: number = 0 175e41f4b71Sopenharmony_ci} 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciclass RouTmp { 178e41f4b71Sopenharmony_ci id: object = () => { 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci info: InfoTmp = new InfoTmp() 181e41f4b71Sopenharmony_ci} 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciconst params: RouTmp = router.getParams() as RouTmp; // Obtain the passed parameter object. 184e41f4b71Sopenharmony_ciconst id: object = params.id // Obtain the value of the id attribute. 185e41f4b71Sopenharmony_ciconst age: number = params.info.age // Obtain the value of the age attribute. 186e41f4b71Sopenharmony_ci``` 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci## Page Return 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ciImplement the page return feature so that users can return to the previous page or a specified page. You can pass parameters to the target page during the return process. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci **Figure 2** Page return 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciBefore using the **Router** module, import it first. 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci```ts 201e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ciYou can use any of the following methods to return to a page: 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci- Method 1: Return to the previous page. 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci ```ts 210e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 211e41f4b71Sopenharmony_ci router.back(); 212e41f4b71Sopenharmony_ci ``` 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci This method allows you to return to the position of the previous page in the page stack. For this method to work, the previous page must exist in the page stack. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci- Method 2: Return to a specific page. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci Return to the page through a common route. 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci ```ts 222e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 223e41f4b71Sopenharmony_ci router.back({ 224e41f4b71Sopenharmony_ci url: 'pages/Home' 225e41f4b71Sopenharmony_ci }); 226e41f4b71Sopenharmony_ci ``` 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci Return to the page through a named route. 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci ```ts 231e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 232e41f4b71Sopenharmony_ci router.back({ 233e41f4b71Sopenharmony_ci url: 'myPage' // myPage is the alias of the page to return to. 234e41f4b71Sopenharmony_ci }); 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci This method allows users to return to a page with the specified path. For this method to work, the target page must exist in the page stack. 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci- Method 3: Return to a specific page and pass custom parameters. 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci Return to the page through a common route. 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci ```ts 245e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 246e41f4b71Sopenharmony_ci router.back({ 247e41f4b71Sopenharmony_ci url: 'pages/Home', 248e41f4b71Sopenharmony_ci params: { 249e41f4b71Sopenharmony_ci info: 'From Home Page' 250e41f4b71Sopenharmony_ci } 251e41f4b71Sopenharmony_ci }); 252e41f4b71Sopenharmony_ci ``` 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci Return to the page through a named route. 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci ```ts 257e41f4b71Sopenharmony_ci import { router } from '@kit.ArkUI'; 258e41f4b71Sopenharmony_ci router.back({ 259e41f4b71Sopenharmony_ci url: 'myPage', // myPage is the alias of the page to return to. 260e41f4b71Sopenharmony_ci params: { 261e41f4b71Sopenharmony_ci info: 'From Home Page' 262e41f4b71Sopenharmony_ci } 263e41f4b71Sopenharmony_ci }); 264e41f4b71Sopenharmony_ci ``` 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci This method not only allows you to return to the specified page, but also pass in custom parameter information during the return process. The parameter information can be obtained and parsed by invoking the **router.getParams** API on the target page. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ciOn the target page, call the **router.getParams** API to obtain parameters at the desired location. For example, you can use it in the [onPageShow](../reference/apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#onpageshow) lifecycle callback. 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci```ts 272e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci@Entry 275e41f4b71Sopenharmony_ci@Component 276e41f4b71Sopenharmony_cistruct Home { 277e41f4b71Sopenharmony_ci @State message: string = 'Hello World'; 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci onPageShow() { 280e41f4b71Sopenharmony_ci const params = router.getParams() as Record<string, string>; // Obtain the passed parameter object. 281e41f4b71Sopenharmony_ci if (params) { 282e41f4b71Sopenharmony_ci const info: string = params.info as string; // Obtain the value of the info attribute. 283e41f4b71Sopenharmony_ci } 284e41f4b71Sopenharmony_ci } 285e41f4b71Sopenharmony_ci ... 286e41f4b71Sopenharmony_ci} 287e41f4b71Sopenharmony_ci``` 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci>**NOTE** 290e41f4b71Sopenharmony_ci> 291e41f4b71Sopenharmony_ci>When the **router.back** API is used to return to a specified page, all pages between the top page (included) and the specified page (excluded) are pushed from the page stack and destroyed. 292e41f4b71Sopenharmony_ci> 293e41f4b71Sopenharmony_ci> If the **router.back** method is used to return to the original page, the original page will not be created repeatedly. Therefore, the variable declared using \@State will not be declared repeatedly, and the **aboutToAppear** lifecycle callback of the page will not be triggered. If you want to use the custom parameters transferred from the returned page on the original page, you can parse the parameters in the required position. For example, parameter parsing can be performed in the **onPageShow** lifecycle callback. 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci## Adding a Confirmation Dialog Box Before Page Return 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ciDuring application development, to prevent misoperations or data loss, a dialog box needs to be displayed before a user returns from one page to another, asking the user whether to perform the operation. 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ciSuch a dialog box can be in the [default style](#default-confirmation-dialog-box) or [custom style](#custom-confirmation-dialog-box). 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci **Figure 3** Adding a confirmation dialog box before page return 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci### Default Confirmation Dialog Box 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ciTo implement this function, you can use the [router.showAlertBeforeBackPage](../reference/apis-arkui/js-apis-router.md#routershowalertbeforebackpage9) and [router.back](../reference/apis-arkui/js-apis-router.md#routerback) APIs provided by the **Router** module. 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ciBefore using the **Router** module, import it first. 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci```ts 315e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 316e41f4b71Sopenharmony_ci``` 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ciTo enable the confirmation dialog box for page return, call the [router.showAlertBeforeBackPage](../reference/apis-arkui/js-apis-router.md#routershowalertbeforebackpage9) API (for setting the information about the dialog box), then the [router.back](../reference/apis-arkui/js-apis-router.md#routerback) API. For example, define a click event processing function for the back button on the payment page: 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci```ts 322e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 323e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci// Define a click event processing function for the back button. 326e41f4b71Sopenharmony_cifunction onBackClick(): void { 327e41f4b71Sopenharmony_ci // Invoke the router.showAlertBeforeBackPage() API to set the information about the confirmation dialog box. 328e41f4b71Sopenharmony_ci try { 329e41f4b71Sopenharmony_ci router.showAlertBeforeBackPage({ 330e41f4b71Sopenharmony_ci message: 'Payment not completed yet. Are you sure you want to return?' // Set the content of the confirmation dialog box. 331e41f4b71Sopenharmony_ci }); 332e41f4b71Sopenharmony_ci } catch (err) { 333e41f4b71Sopenharmony_ci let message = (err as BusinessError).message 334e41f4b71Sopenharmony_ci let code = (err as BusinessError).code 335e41f4b71Sopenharmony_ci console.error(`Invoke showAlertBeforeBackPage failed, code is ${code}, message is ${message}`); 336e41f4b71Sopenharmony_ci } 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci // Invoke the router.back() API to return to the previous page. 339e41f4b71Sopenharmony_ci router.back(); 340e41f4b71Sopenharmony_ci} 341e41f4b71Sopenharmony_ci``` 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ciThe **router.showAlertBeforeBackPage** API receives an object as a parameter. The object contains the following attributes: 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci**message**: content of the dialog box. The value is of the string type. 346e41f4b71Sopenharmony_ciIf the API is called successfully, the confirmation dialog box is displayed on the target page. Otherwise, an exception is thrown and the error code and error information is obtained through **err.code** and **err.message**. 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ciWhen the user clicks the back button, a confirmation dialog box is displayed, prompting the user to confirm their operation. If the user selects **Cancel**, the application stays on the current page. If the user selects OK, the **router.back** API is called and the redirection is performed based on the parameters. 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci### Custom Confirmation Dialog Box 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ciTo implement a custom confirmation dialog box, use APIs in the [promptAction.showDialog](../reference/apis-arkui/js-apis-promptAction.md#promptactionshowdialog) module or create a custom dialog box . This topic uses the APIs in the **PromptAction** module an example to describe how to implement a custom confirmation dialog box. 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ciBefore using the **Router** module, import it first. 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci```ts 358e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 359e41f4b71Sopenharmony_ci``` 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ciIn the event callback, call the [promptAction.showDialog](../reference/apis-arkui/js-apis-promptAction.md#promptactionshowdialog) API of the **PromptAction** module. 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci```ts 365e41f4b71Sopenharmony_ciimport { promptAction, router } from '@kit.ArkUI'; 366e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_cifunction onBackClick() { 369e41f4b71Sopenharmony_ci // Display a custom confirmation dialog box. 370e41f4b71Sopenharmony_ci promptAction.showDialog({ 371e41f4b71Sopenharmony_ci message:'Payment not completed yet. Are you sure you want to return?', 372e41f4b71Sopenharmony_ci buttons: [ 373e41f4b71Sopenharmony_ci { 374e41f4b71Sopenharmony_ci text: 'Cancel', 375e41f4b71Sopenharmony_ci color: '#FF0000' 376e41f4b71Sopenharmony_ci }, 377e41f4b71Sopenharmony_ci { 378e41f4b71Sopenharmony_ci text: 'OK', 379e41f4b71Sopenharmony_ci color: '#0099FF' 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci ] 382e41f4b71Sopenharmony_ci }).then((result:promptAction.ShowDialogSuccessResponse) => { 383e41f4b71Sopenharmony_ci if (result.index === 0) { 384e41f4b71Sopenharmony_ci // The user selects Cancel. 385e41f4b71Sopenharmony_ci console.info('User canceled the operation.'); 386e41f4b71Sopenharmony_ci } else if (result.index === 1) { 387e41f4b71Sopenharmony_ci // The user selects OK. 388e41f4b71Sopenharmony_ci console.info('User confirmed the operation.'); 389e41f4b71Sopenharmony_ci // Invoke the router.back() API to return to the previous page. 390e41f4b71Sopenharmony_ci router.back(); 391e41f4b71Sopenharmony_ci } 392e41f4b71Sopenharmony_ci }).catch((err:Error) => { 393e41f4b71Sopenharmony_ci let message = (err as BusinessError).message 394e41f4b71Sopenharmony_ci let code = (err as BusinessError).code 395e41f4b71Sopenharmony_ci console.error(`Invoke showDialog failed, code is ${code}, message is ${message}`); 396e41f4b71Sopenharmony_ci }) 397e41f4b71Sopenharmony_ci} 398e41f4b71Sopenharmony_ci``` 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ciWhen the user clicks the back button, the custom confirmation dialog box is displayed, prompting the user to confirm their operation. If the user selects **Cancel**, the application stays on the current page. If the user selects OK, the **router.back** API is called and the redirection is performed based on the parameters. 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci## Named Route 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ciTo redirect to a page in a [HAR](../quick-start/har-package.md) or [HSP](../quick-start/in-app-hsp.md), you can use [router.pushNamedRoute](../reference/apis-arkui/js-apis-router.md#routerpushnamedroute10). 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci **Figure 4** Named route redirection 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciBefore using the **Router** module, import it first. 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci```ts 414e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 415e41f4b71Sopenharmony_ci``` 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ciIn the [HAR](../quick-start/har-package.md) or [HSP](../quick-start/in-app-hsp.md) you want to navigate to, name the @Entry decorated custom component in [EntryOptions](../quick-start/arkts-create-custom-components.md#entryoptions10). 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci```ts 420e41f4b71Sopenharmony_ci// library/src/main/ets/pages/Index.ets 421e41f4b71Sopenharmony_ci// library is the custom name of the new shared package. 422e41f4b71Sopenharmony_ci@Entry({ routeName: 'myPage' }) 423e41f4b71Sopenharmony_ci@Component 424e41f4b71Sopenharmony_ciexport struct MyComponent { 425e41f4b71Sopenharmony_ci build() { 426e41f4b71Sopenharmony_ci Row() { 427e41f4b71Sopenharmony_ci Column() { 428e41f4b71Sopenharmony_ci Text('Library Page') 429e41f4b71Sopenharmony_ci .fontSize(50) 430e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 431e41f4b71Sopenharmony_ci } 432e41f4b71Sopenharmony_ci .width('100%') 433e41f4b71Sopenharmony_ci } 434e41f4b71Sopenharmony_ci .height('100%') 435e41f4b71Sopenharmony_ci } 436e41f4b71Sopenharmony_ci} 437e41f4b71Sopenharmony_ci``` 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ciWhen the configuration is successful, import the named route page to the page from which you want to redirect. 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci```ts 442e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 443e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 444e41f4b71Sopenharmony_ciimport '@ohos/library/src/main/ets/pages/Index'; // Import the named route page from the library of the shared package. 445e41f4b71Sopenharmony_ci@Entry 446e41f4b71Sopenharmony_ci@Component 447e41f4b71Sopenharmony_cistruct Index { 448e41f4b71Sopenharmony_ci build() { 449e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 450e41f4b71Sopenharmony_ci Text('Hello World') 451e41f4b71Sopenharmony_ci .fontSize(50) 452e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 453e41f4b71Sopenharmony_ci .margin({ top: 20 }) 454e41f4b71Sopenharmony_ci .backgroundColor('#ccc') 455e41f4b71Sopenharmony_ci .onClick(() => { // Click to go to a page in another shared package. 456e41f4b71Sopenharmony_ci try { 457e41f4b71Sopenharmony_ci router.pushNamedRoute({ 458e41f4b71Sopenharmony_ci name: 'myPage', 459e41f4b71Sopenharmony_ci params: { 460e41f4b71Sopenharmony_ci data1: 'message', 461e41f4b71Sopenharmony_ci data2: { 462e41f4b71Sopenharmony_ci data3: [123, 456, 789] 463e41f4b71Sopenharmony_ci } 464e41f4b71Sopenharmony_ci } 465e41f4b71Sopenharmony_ci }) 466e41f4b71Sopenharmony_ci } catch (err) { 467e41f4b71Sopenharmony_ci let message = (err as BusinessError).message 468e41f4b71Sopenharmony_ci let code = (err as BusinessError).code 469e41f4b71Sopenharmony_ci console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`); 470e41f4b71Sopenharmony_ci } 471e41f4b71Sopenharmony_ci }) 472e41f4b71Sopenharmony_ci } 473e41f4b71Sopenharmony_ci .width('100%') 474e41f4b71Sopenharmony_ci .height('100%') 475e41f4b71Sopenharmony_ci } 476e41f4b71Sopenharmony_ci} 477e41f4b71Sopenharmony_ci``` 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci>**NOTE** 480e41f4b71Sopenharmony_ci> 481e41f4b71Sopenharmony_ci>To use the named route for redirection, you must configure dependencies in the **oh-package.json5** file of the application package. Example: 482e41f4b71Sopenharmony_ci> 483e41f4b71Sopenharmony_ci>```ts 484e41f4b71Sopenharmony_ci>"dependencies": { 485e41f4b71Sopenharmony_ci> "@ohos/library": "file:../library", 486e41f4b71Sopenharmony_ci> ... 487e41f4b71Sopenharmony_ci> } 488e41f4b71Sopenharmony_ci>``` 489