HarmonyOS string.json 资源字符串常量拼接问题

https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkts-11-V5 中,可以实现 字符串常量 在Component 组件中使用,但是如何在获取到对应的变量,例如:

let r = $r('app.string.module_desc', 12345) 
let a = context.resourceManager.getStringSync(r); 
  • 1.
  • 2.

然后在其他地方使用呢?

HarmonyOS
2025-01-09 14:44:34
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

示例如下:

import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext;
  private str:string = ""
  aboutToAppear(): void {
    try {
      this.str = this.context.resourceManager.getStringSync($r('app.string.xxx_error_message').id,10005);
    } catch (error) {
      let code = (error as BusinessError).code;
      let message = (error as BusinessError).message;
      console.error(getStringSync failed, error code: ${code}, message: ${message}.);
    }
  }
  build() {
    RelativeContainer() {
      Text(this.str)
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
分享
微博
QQ
微信
回复
2025-01-09 17:37:20


相关问题
HarmonyOS string字符串截取问题
633浏览 • 1回复 待解决
嵌套JSON字符串解析问题
2510浏览 • 1回复 待解决
如何实现ArkUI组件字符串变量拼接
941浏览 • 1回复 待解决
如何实现ArkUI组件字符串变量拼接
3318浏览 • 1回复 待解决
应用开发时string.json问题
1021浏览 • 1回复 待解决
关于鸿蒙arkts解析json字符串问题
1268浏览 • 2回复 待解决
HarmonyOS 超长JSON字符串解析
793浏览 • 2回复 待解决
HarmonyOS string类型的字符串截取
1450浏览 • 1回复 待解决
$r('xxxxx')是否支持字符串动态拼接
2695浏览 • 1回复 待解决
HarmonyOS 服务端JSON字符串解析问题
946浏览 • 1回复 待解决
如何打印JSON对象和JSON字符串
877浏览 • 1回复 待解决
HarmonyOS 如何解析json字符串
1295浏览 • 1回复 待解决