• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {Callback} from './basic';
17import { AsyncCallback } from './basic';
18
19/**
20 * @namespace router
21 * @syscap SystemCapability.ArkUI.ArkUI.Full
22 * @since 8
23 */
24declare namespace router {
25
26  /**
27   * Router Mode
28   * @syscap SystemCapability.ArkUI.ArkUI.Full
29   * @since 9
30   */
31  export enum RouterMode {
32    /**
33     * Default route mode.
34     * The page will be added to the top of the page stack.
35     * @since 9
36     */
37    Standard,
38
39    /**
40     * Single route mode.
41     * If the target page already has the same url page in the page stack,
42     * the same url page closest to the top of the stack will be moved to the top of the stack.
43     * If the target page url does not exist in the page stack, route will use default route mode.
44     * @since 9
45     */
46    Single,
47  }
48
49  /**
50   * @typedef RouterOptions
51   * @syscap SystemCapability.ArkUI.ArkUI.Lite
52   * @since 8
53   */
54  interface RouterOptions {
55
56    /**
57     * URI of the destination page, which supports the following formats:
58     * 1. Absolute path of the page, which is provided by the pages list in the config.json file.
59     *    Example:
60     *      pages/index/index
61     *      pages/detail/detail
62     * 2. Particular path. If the URI is a slash (/), the home page is displayed.
63     * @syscap SystemCapability.ArkUI.ArkUI.Lite
64     * @type {string}
65     * @since 8
66     */
67    url: string;
68
69    /**
70     * Data that needs to be passed to the destination page during navigation.
71     * After the destination page is displayed, the parameter can be directly used for the page.
72     * For example, this.data1 (data1 is the key value of the params used for page navigation.)
73     * @syscap SystemCapability.ArkUI.ArkUI.Lite
74     * @type {Object}
75     * @since 8
76     */
77    params?: Object;
78  }
79
80  /**
81   * @typedef RouterState
82   * @syscap SystemCapability.ArkUI.ArkUI.Full
83   * @since 8
84   */
85  interface RouterState {
86
87    /**
88     * Index of the current page in the stack.
89     * NOTE: The index starts from 1 from the bottom to the top of the stack.
90     * @type {number}
91     * @since 8
92     */
93    index: number;
94
95    /**
96     * Name of the current page, that is, the file name.
97     * @type {string}
98     * @since 8
99     */
100    name: string;
101
102    /**
103     * Path of the current page.
104     * @type {string}
105     * @since 8
106     */
107    path: string;
108  }
109
110  /**
111   * @typedef EnableAlertOptions
112   * @syscap SystemCapability.ArkUI.ArkUI.Full
113   * @since 8
114   */
115  interface EnableAlertOptions {
116
117    /**
118     * dialog context.
119     * @type {string}
120     * @since 8
121     */
122    message: string;
123  }
124
125  /**
126   * Navigates to a specified page in the application based on the page URL and parameters.
127   * @param { RouterOptions } options - Options.
128   * @syscap SystemCapability.ArkUI.ArkUI.Full
129   * @since 8
130   * @deprecated since 9
131   * @useinstead ohos.router.router#pushUrl
132   */
133  function push(options: RouterOptions):void;
134
135  /**
136   * Navigates to a specified page in the application based on the page URL and parameters.
137   * @param { RouterOptions } options - Options.
138   * @param { AsyncCallback<void> } callback - the callback of pushUrl.
139   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
140   * @throws { BusinessError } 100001 - if UI execution context not found.
141   * @throws { BusinessError } 100002 - if the uri is not exist.
142   * @throws { BusinessError } 100003 - if the pages are pushed too much.
143   * @syscap SystemCapability.ArkUI.ArkUI.Full
144   * @since 9
145   */
146  function pushUrl(options: RouterOptions, callback: AsyncCallback<void>):void;
147
148  /**
149   * Navigates to a specified page in the application based on the page URL and parameters.
150   * @param { RouterOptions } options - Options.
151   * @returns { Promise<void> } the promise returned by the function.
152   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
153   * @throws { BusinessError } 100001 - if UI execution context not found.
154   * @throws { BusinessError } 100002 - if the uri is not exist.
155   * @throws { BusinessError } 100003 - if the pages are pushed too much.
156   * @syscap SystemCapability.ArkUI.ArkUI.Full
157   * @since 9
158   */
159  function pushUrl(options: RouterOptions): Promise<void>;
160
161   /**
162   * Navigates to a specified page in the application based on the page URL and parameters.
163   * @param { RouterOptions } options - Options.
164   * @param { RouterMode } mode - RouterMode.
165   * @param { AsyncCallback<void> } callback - the callback of pushUrl.
166   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
167   * @throws { BusinessError } 100001 - if UI execution context not found.
168   * @throws { BusinessError } 100002 - if the uri is not exist.
169   * @throws { BusinessError } 100003 - if the pages are pushed too much.
170   * @syscap SystemCapability.ArkUI.ArkUI.Full
171   * @since 9
172   */
173  function pushUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>):void;
174
175  /**
176   * Navigates to a specified page in the application based on the page URL and parameters.
177   * @param { RouterOptions } options - Options.
178   * @param { RouterMode } mode - RouterMode.
179   * @returns { Promise<void> } the promise returned by the function.
180   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
181   * @throws { BusinessError } 100001 - if UI execution context not found.
182   * @throws { BusinessError } 100002 - if the uri is not exist.
183   * @throws { BusinessError } 100003 - if the pages are pushed too much.
184   * @syscap SystemCapability.ArkUI.ArkUI.Full
185   * @since 9
186   */
187  function pushUrl(options: RouterOptions, mode: RouterMode): Promise<void>;
188
189  /**
190   * Replaces the current page with another one in the application. The current page is destroyed after replacement.
191   * @param { RouterOptions } options - Options.
192   * @syscap SystemCapability.ArkUI.ArkUI.Lite
193   * @since 8
194   * @deprecated since 9
195   * @useinstead ohos.router.router#replaceUrl
196   */
197  function replace(options: RouterOptions):void;
198
199  /**
200   * Replaces the current page with another one in the application. The current page is destroyed after replacement.
201   * @param { RouterOptions } options - Options.
202   * @param { AsyncCallback<void> } callback - the callback of replaceUrl.
203   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
204   * @throws { BusinessError } 100001 - if UI execution context not found, only throw in standard system.
205   * @throws { BusinessError } 200002 - if the uri is not exist.
206   * @syscap SystemCapability.ArkUI.ArkUI.Lite
207   * @since 9
208   */
209  function replaceUrl(options: RouterOptions, callback: AsyncCallback<void>):void;
210
211  /**
212   * Replaces the current page with another one in the application. The current page is destroyed after replacement.
213   * @param { RouterOptions } options - Options.
214   * @returns { Promise<void> } the promise returned by the function.
215   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
216   * @throws { BusinessError } 100001 - if UI execution context not found, only throw in standard system.
217   * @throws { BusinessError } 200002 - if the uri is not exist.
218   * @syscap SystemCapability.ArkUI.ArkUI.Lite
219   * @since 9
220   */
221  function replaceUrl(options: RouterOptions): Promise<void>;
222
223  /**
224   * Replaces the current page with another one in the application. The current page is destroyed after replacement.
225   * @param { RouterOptions } options - Options.
226   * @param { RouterMode } mode - RouterMode.
227   * @param { AsyncCallback<void> } callback - the callback of replaceUrl.
228   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
229   * @throws { BusinessError } 100001 - if UI execution context not found, only throw in standard system.
230   * @throws { BusinessError } 200002 - if the uri is not exist.
231   * @syscap SystemCapability.ArkUI.ArkUI.Lite
232   * @since 9
233   */
234  function replaceUrl(options: RouterOptions, mode: RouterMode, callback: AsyncCallback<void>):void;
235
236  /**
237   * Replaces the current page with another one in the application. The current page is destroyed after replacement.
238   * @param { RouterOptions } options - Options.
239   * @param { RouterMode } mode - RouterMode.
240   * @returns { Promise<void> } the promise returned by the function.
241   * @throws { BusinessError } 401 - if the number of parameters is less than 1 or the type of the url parameter is not string.
242   * @throws { BusinessError } 100001 - if can not get the delegate, only throw in standard system.
243   * @throws { BusinessError } 200002 - if the uri is not exist.
244   * @syscap SystemCapability.ArkUI.ArkUI.Lite
245   * @since 9
246   */
247  function replaceUrl(options: RouterOptions, mode: RouterMode): Promise<void>;
248
249  /**
250   * Returns to the previous page or a specified page.
251   * @param { RouterOptions } options - Options.
252   * @syscap SystemCapability.ArkUI.ArkUI.Full
253   * @since 8
254   */
255  function back(options?: RouterOptions):void;
256
257  /**
258   * Clears all historical pages and retains only the current page at the top of the stack.
259   * @syscap SystemCapability.ArkUI.ArkUI.Full
260   * @since 8
261   */
262  function clear():void;
263
264  /**
265   * Obtains the number of pages in the current stack.
266   * @returns { string } Number of pages in the stack. The maximum value is 32.
267   * @syscap SystemCapability.ArkUI.ArkUI.Full
268   * @since 8
269   */
270  function getLength():string;
271
272  /**
273   * Obtains information about the current page state.
274   * @returns { RouterState }Page state.
275   * @syscap SystemCapability.ArkUI.ArkUI.Full
276   * @since 8
277   */
278  function getState():RouterState;
279
280  /**
281   * Pop up dialog to ask whether to back
282   * @param { EnableAlertOptions } options - Options.
283   * @syscap SystemCapability.ArkUI.ArkUI.Full
284   * @since 8
285   * @deprecated since 9
286   * @useinstead ohos.router.router#showAlertBeforeBackPage
287   */
288  function enableAlertBeforeBackPage(options: EnableAlertOptions):void;
289
290  /**
291   * Pop up alert dialog to ask whether to back
292   * @param { EnableAlertOptions } options - Options.
293   * @throws { BusinessError } 401 - if the type of the parameter is not object or the type of the message is not string.
294   * @throws { BusinessError } 100001 - if UI execution context not found.
295   * @syscap SystemCapability.ArkUI.ArkUI.Full
296   * @since 9
297   */
298  function showAlertBeforeBackPage(options: EnableAlertOptions):void;
299
300  /**
301   * Cancel enableAlertBeforeBackPage
302   * @syscap SystemCapability.ArkUI.ArkUI.Full
303   * @since 8
304   * @deprecated since 9
305   * @useinstead ohos.router.router#hideAlertBeforeBackPage
306   */
307  function disableAlertBeforeBackPage():void;
308
309  /**
310   * Hide alert before back page
311   * @syscap SystemCapability.ArkUI.ArkUI.Full
312   * @since 9
313   */
314   function hideAlertBeforeBackPage():void;
315
316  /**
317   * Obtains information about the current page params.
318   * @returns { Object }Page params.
319   * @syscap SystemCapability.ArkUI.ArkUI.Full
320   * @since 8
321   */
322  function getParams(): Object;
323}
324
325export default router;
326