HarmonyOS 可以直接引用配置文件做业务操作嘛?

采用的Navigation组件,路由采用navPathStack的系统路由,现在想要做路由页面是否需要鉴权,直接引用resource目录下的系统配置json文件。根据json文件中routerMap-\>data-\>auth字段进行判断,具体代码详见下图

阅读 509
1 个回答

profile目录下也是这边也是提供了获取方法的bundleManager.getProfileByAbilitySync,注意一下配置一下module.json中metadata标签,参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-bundlemanager-V5\#bundlemanagergetprofilebyabilitysync10

参考demo:

//module.json5中abilities下

"metadata": [{
  "name": "ability_metadata",
  "value": "a test demo for ability",
  "resource": "$profile:test"
}],
import bundleManager from '@ohos.bundle.bundleManager';
import { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';

@Entry
@Component
struct Index {
  @State message: string = '获取profile文件';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let moduleName = 'entry';
            let abilityName = 'EntryAbility';
            let metadataName: string = 'ability_metadata';

            try {
              let data = bundleManager.getProfileByAbilitySync(moduleName, abilityName, metadataName);
              hilog.info(0x0000, 'testTag', 'getProfileByAbilitySync successfully. Data: %{public}s', JSON.stringify(data));
            } catch (err) {
              let message = (err as BusinessError).message;
              hilog.error(0x0000, 'testTag', 'getProfileByAbilitySync failed. Cause: %{public}s', message);
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏