diff --git a/zh-cn/application-dev/application-models/start-page.md b/zh-cn/application-dev/application-models/start-page.md index ca1ce7a7ec6681573a77fb3e24bca499a529e23b..87e721fd6c871824e3b6e2c82df2f5087a37bdc2 100644 --- a/zh-cn/application-dev/application-models/start-page.md +++ b/zh-cn/application-dev/application-models/start-page.md @@ -30,7 +30,7 @@ async function restartAbility() { 在目标端PageAbility的onNewWant回调中获取包含页面信息的want参数: ```ts -import Want from '@ohos.app.ability.Want'; +// GlobalContext.ts 构造单例对象 export class GlobalContext { private constructor() {} private static instance: GlobalContext; @@ -51,6 +51,11 @@ export class GlobalContext { this._objects.set(key, objectClass); } } +``` + +```ts +import Want from '@ohos.application.Want'; +import { GlobalContext } from './GlobalContext'; export default class EntryAbility{ onNewWant(want: Want) { @@ -63,8 +68,9 @@ export default class EntryAbility{ 在目标端页面的自定义组件中获取包含页面信息的want参数并根据uri做路由处理: ```ts +import Want from '@ohos.application.Want'; import router from '@ohos.router'; -import { GlobalContext } from '../GlobalContext' +import { GlobalContext } from '../GlobalContext'; @Entry @Component @@ -73,10 +79,12 @@ struct Index { onPageShow() { console.info('Index onPageShow') - let newWant: Want = GlobalContext.getContext().getObject("newWant") - if (newWant.hasOwnProperty("page")) { - router.push({ url: newWant.page }); - GlobalContext.getContext().setObject("newWant", undefined) + let newWant = GlobalContext.getContext().getObject("newWant") as Want + if (newWant.parameters) { + if (newWant.parameters.page) { + router.push({ url: newWant.parameters.page }); + GlobalContext.getContext().setObject("newWant", undefined) + } } } @@ -160,7 +168,7 @@ export default class EntryAbility { if (want.parameters) { if (want.parameters.page) { router.push({ - url: want.parameters.page + url: want.parameters.page as string }) } } diff --git a/zh-cn/application-dev/reference/apis/js-apis-app-ability-abilityLifecycleCallback.md b/zh-cn/application-dev/reference/apis/js-apis-app-ability-abilityLifecycleCallback.md index c2f4832377f640afb63eb7aca88ae223554f66e6..4ebd7260ac550b60845e52c523f06664267f0179 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-app-ability-abilityLifecycleCallback.md +++ b/zh-cn/application-dev/reference/apis/js-apis-app-ability-abilityLifecycleCallback.md @@ -333,7 +333,7 @@ import { GlobalContext } from '../GlobalContext' export default class MySecondAbility extends UIAbility { onDestroy() { let applicationContext = this.context.getApplicationContext(); - let lifecycleId: number = GlobalContext.getContext().getObject("lifecycleId"); + let lifecycleId = GlobalContext.getContext().getObject("lifecycleId") as number; // 3.通过applicationContext注销监听应用内生命周期 applicationContext.off('abilityLifecycle', lifecycleId, (error) => { if (error && error.code !== 0) { diff --git a/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md b/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md index 68e1ff54da71d5237c9bde4cdf6d6e3e69774453..ae43d32c570192d5ce45a3b2e32bf9c1e2e65eb6 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md +++ b/zh-cn/application-dev/reference/apis/js-apis-app-ability-applicationStateChangeCallback.md @@ -27,53 +27,30 @@ onApplicationForeground(): void; import UIAbility from '@ohos.app.ability.UIAbility'; import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback'; -// 构造单例对象 -export class GlobalContext { - private constructor() {} - private static instance: GlobalContext; - private _objects = new Map(); - - public static getContext(): GlobalContext { - if (!GlobalContext.instance) { - GlobalContext.instance = new GlobalContext(); - } - return GlobalContext.instance; - } - - getObject(value: string): Object | undefined { - return this._objects.get(value); - } - - setObject(key: string, objectClass: Object): void { - this._objects.set(key, objectClass); - } -} - let applicationStateChangeCallback: ApplicationStateChangeCallback = { onApplicationForeground() { console.info('applicationStateChangeCallback onApplicationForeground'); + }, + onApplicationBackground() { + console.info('applicationStateChangeCallback onApplicationBackground'); } } -GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback); export default class MyAbility extends UIAbility { onCreate() { console.log('MyAbility onCreate'); - GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext()); // 1.获取applicationContext - let applicationContext = GlobalContext.getContext().getObject("applicationContext"); + let applicationContext = this.context.getApplicationContext(); // 2.通过applicationContext注册应用前后台状态监听 if (applicationContext != undefined) { - applicationContext.on('applicationStateChange', - GlobalContext.getContext().getObject("applicationStateChangeCallback")); + applicationContext.on('applicationStateChange', applicationStateChangeCallback); } } onDestroy() { - let applicationContext = GlobalContext.getContext().getObject("applicationContext"); + let applicationContext = this.context.getApplicationContext(); // 1.通过applicationContext解除注册应用前后台状态监听 if (applicationContext != undefined) { - applicationContext.off('applicationStateChange', - GlobalContext.getContext().getObject("applicationStateChangeCallback")); + applicationContext.off('applicationStateChange', applicationStateChangeCallback); } } } @@ -93,53 +70,31 @@ onApplicationBackground(): void; import UIAbility from '@ohos.app.ability.UIAbility'; import ApplicationStateChangeCallback from '@ohos.app.ability.ApplicationStateChangeCallback'; -export class GlobalContext { - private constructor() {} - private static instance: GlobalContext; - private _objects = new Map(); - - public static getContext(): GlobalContext { - if (!GlobalContext.instance) { - GlobalContext.instance = new GlobalContext(); - } - return GlobalContext.instance; - } - - getObject(value: string): Object | undefined { - return this._objects.get(value); - } - - setObject(key: string, objectClass: Object): void { - this._objects.set(key, objectClass); - } -} - let applicationStateChangeCallback: ApplicationStateChangeCallback = { + onApplicationForeground() { + console.info('applicationStateChangeCallback onApplicationForeground'); + }, onApplicationBackground() { console.info('applicationStateChangeCallback onApplicationBackground'); } } -GlobalContext.getContext().setObject("applicationStateChangeCallback", applicationStateChangeCallback); export default class MyAbility extends UIAbility { onCreate() { console.log('MyAbility onCreate'); - GlobalContext.getContext().setObject("applicationContext", this.context.getApplicationContext()); // 1.获取applicationContext - let applicationContext = GlobalContext.getContext().getObject("applicationContext"); + let applicationContext = this.context.getApplicationContext(); // 2.通过applicationContext注册应用前后台状态监听 if (applicationContext != undefined) { - applicationContext.on('applicationStateChange', - GlobalContext.getContext().getObject("applicationStateChangeCallback")); + applicationContext.on('applicationStateChange', applicationStateChangeCallback); } console.log('Resgiter applicationStateChangeCallback'); } onDestroy() { - let applicationContext = GlobalContext.getContext().getObject("applicationContext"); + let applicationContext = this.context.getApplicationContext(); // 1.通过applicationContext解除注册应用前后台状态监听 if (applicationContext != undefined) { - applicationContext.off('applicationStateChange', - GlobalContext.getContext().getObject("applicationStateChangeCallback")); + applicationContext.off('applicationStateChange', applicationStateChangeCallback); } } } diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-extensionContext.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-extensionContext.md index 5ea5530b24c91df3f5e66d27e1a0bb3f239c4bb0..5566ea169ea6a180c0be3945df2bc66a9bbe9c3d 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-extensionContext.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-extensionContext.md @@ -37,10 +37,7 @@ ExtensionContext主要用于查询所属Extension的信息、Module的配置信 三个Module内都定义一个相同名称的ServiceExtension: ```ts -import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; -import Want from '@ohos.app.ability.Want'; -import rpc from '@ohos.rpc'; - +// 单例对象 GlobalContext.ts export class GlobalContext { private constructor() {} private static instance: GlobalContext; @@ -61,6 +58,13 @@ export class GlobalContext { this._objects.set(key, objectClass); } } +``` + +```ts +import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; +import Want from '@ohos.app.ability.Want'; +import rpc from '@ohos.rpc'; +import { GlobalContext } from '../GlobalContext' export default class TheServiceExtension extends ServiceExtension { onCreate(want: Want) { @@ -109,7 +113,8 @@ export default class EntryAbility extends UIAbility { 在entry内新建一个ServiceModule.ts,专用于执行业务逻辑 ```ts -import { GlobalContext } from '../TheServiceExtension' +import common from '@ohos.app.ability.common'; +import { GlobalContext } from '../GlobalContext'; export default class ServiceModel { moduleName: string = ''; @@ -122,7 +127,8 @@ export default class ServiceModel { return; } - this.moduleName = GlobalContext.getContext().getObject('ExtensionContext').currentHapModuleInfo.name; + let extensionContext = GlobalContext.getContext().getObject('ExtensionContext') as common.ServiceExtensionContext; + this.moduleName = extensionContext.currentHapModuleInfo.name; // 根据moduleName执行不同的业务逻辑,实现对不同性能设备的区分 switch (this.moduleName) { case 'highPerformance':