diff --git a/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md b/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md index 948eb9a341774b3d717489f6b80a0136966716b5..25a6f0c8a00da59860244c7159a4b278bf6ccbe8 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md +++ b/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md @@ -21,15 +21,65 @@ 通过atManager实例来获取。 **示例:** + +ArkTS语法不支持直接使用globalThis,需要通过一个单例的map来做中转。开发者需要: + +a. 在EntryAbility.ets中导入构建的单例对象GlobalThis。 + ```typescript + import { GlobalThis } from '../utils/globalThis'; // 需要根据globalThis.ets的路径自行适配 + ``` +b. 在onCreate中添加: + ```typescript + GlobalThis.getInstance().setContext('context', this.context); + ``` + +> **说明:** +> +> 由于在ts中引入ets文件会有告警提示,需要将EntryAbility.ts的文件后缀修改为EntryAbility.ets,并在module.json5中同步修改。 + +**globalThis.ets示例代码如下:** +```typescript +import common from '@ohos.app.ability.common'; + +// 构造单例对象 +export class GlobalThis { + private constructor() {} + private static instance: GlobalThis; + private _uiContexts = new Map(); + + public static getInstance(): GlobalThis { + if (!GlobalThis.instance) { + GlobalThis.instance = new GlobalThis(); + } + return GlobalThis.instance; + } + + getContext(key: string): common.UIAbilityContext | undefined { + return this._uiContexts.get(key); + } + + setContext(key: string, value: common.UIAbilityContext): void { + this._uiContexts.set(key, value); + } + + // 其他需要传递的内容依此扩展 +} +``` + ```ts +import { BusinessError } from '@ohos.base'; import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; +import common from '@ohos.app.ability.common'; +import { GlobalThis } from '../utils/globalThis'; + let atManager = abilityAccessCtrl.createAtManager(); try { - atManager.requestPermissionsFromUser(this.context, ["ohos.permission.CAMERA"]).then((data) => { + let context: common.UIAbilityContext = GlobalThis.getInstance().getContext('context'); + atManager.requestPermissionsFromUser(context, ["ohos.permission.CAMERA"]).then((data) => { console.info("data:" + JSON.stringify(data)); console.info("data permissions:" + data.permissions); console.info("data authResults:" + data.authResults); - }).catch((err) => { + }).catch((err: BusinessError) => { console.info("data:" + JSON.stringify(err)); }) } catch(err) {