From 5abe8667eb35f24199584ab68bbf5d165c99061f Mon Sep 17 00:00:00 2001 From: "ester.zhou" Date: Mon, 15 May 2023 11:18:13 +0800 Subject: [PATCH] Update doc (16796) Signed-off-by: ester.zhou --- .../arkui-js-lite/js-framework-js-file.md | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/en/application-dev/reference/arkui-js-lite/js-framework-js-file.md b/en/application-dev/reference/arkui-js-lite/js-framework-js-file.md index d22c607252..17d26d717f 100644 --- a/en/application-dev/reference/arkui-js-lite/js-framework-js-file.md +++ b/en/application-dev/reference/arkui-js-lite/js-framework-js-file.md @@ -1,5 +1,6 @@ # app.js +## Application Lifecycle4+ You can implement lifecycle logic specific to your application in the **app.js** file. Available application lifecycle functions are as follows: @@ -11,7 +12,9 @@ You can implement lifecycle logic specific to your application in the **app.js** In the following example, logs are printed only in the lifecycle functions. -``` + + +```js // app.js export default { onCreate() { @@ -22,3 +25,63 @@ export default { }, } ``` + +## Application Object10+ + +| Attribute | Type | Description | +| ------ | -------- | ---------------------------------------- | +| getApp | Function | Obtains the data object exposed in **app.js** from the page JS file. This API works globally.| + +> **NOTE** +> +> The application object is global data and occupies JS memory before the application exits. Although it facilitates data sharing between different pages, exercise caution when using it on small-system devices, whose memory is small. If they are overused, exceptions may occur due to insufficient memory when the application displays complex pages. + +The following is an example: + +Declare the application object in **app.js**. + +```javascript +// app.js +export default { + data: { + test: "by getAPP" + }, + onCreate() { + console.info('Application onCreate'); + }, + onDestroy() { + console.info('Application onDestroy'); + }, +}; +``` + +Access the application object on a specific page. + +```javascript +// index.js +export default { + data: { + title: "" + }, + onInit() { + if (typeof getApp !== 'undefined') { + var appData = getApp().data; + if (typeof appData !== 'undefined') { + this.title = appData.name; // read from app data + } + } + }, + clickHandler() { + if (typeof getApp !== 'undefined') { + var appData = getApp().data; + if (typeof appData !== 'undefined') { + appData.name = this.title; // write to app data + } + } + } +} +``` + +> **NOTE** +> +> To ensure that the application can run properly on an earlier version that does not support **getApp**, compatibility processing must be performed in the code. That is, before using **getApp**, check whether it is available. -- GitLab