HarmonyOS ResourceStr输出string

把string定义在资源文件中,使用ResourceStr ,现在想获取buttonText的string内容,和服务器的string进行比对,要怎么输出buttonText的string内容。

HarmonyOS
2024-12-20 15:57:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

使用getStringSync方法。参考地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5#ZH-CN_TOPIC_0000001847052344__getstringsync9-1

demo如下:

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

@Entry
@Component
struct Index2 {
  @State message: string = 'Hello World';
  @State buttonText: Resource = $r('app.string.caijiOnly')
  context = getContext(this) as common.UIAbilityContext;

  aboutToAppear(): void {
    let resource: resourceManager.Resource = {
      bundleName: "com.example.mydemo", //修改为自己的包名
      moduleName: "entry",
      id: $r('app.string.caijiOnly').id
    };
    try {
      this.context.resourceManager.getStringSync(resource);
      console.log("官网示例:" + this.context.resourceManager.getStringSync(resource))
      console.log("buttonText:" + this.context.resourceManager.getStringSync(this.buttonText))

    } catch (error) {
      let code = (error as BusinessError).code;
      let message = (error as BusinessError).message;
      console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
    }

  }

  build() {
    Column() {
      Text(this.message)
    }
  }
}
  • 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.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
分享
微博
QQ
微信
回复
2024-12-20 18:03:25
相关问题
ResourceStr 类型如何转成字符串?
1440浏览 • 1回复 待解决
HarmonyOS 视频硬解输出
891浏览 • 1回复 待解决
HarmonyOS string相关问题
938浏览 • 1回复 待解决
HarmonyOS release下日志不输出
1293浏览 • 1回复 待解决
HarmonyOS string 转 number
1756浏览 • 1回复 待解决
HarmonyOS string 包含,替换
543浏览 • 1回复 待解决
Record<string, string>如何删除里边的元素
2284浏览 • 1回复 待解决
如何创建相机预览输出
951浏览 • 0回复 待解决
HarmonyOS string、number扩展方法
635浏览 • 0回复 待解决
HarmonyOS string没有replaceAll方法
1649浏览 • 1回复 待解决
HarmonyOS String如何转Int
838浏览 • 1回复 待解决
HarmonyOS如何控制组件的属性输出
898浏览 • 1回复 待解决
openharmony: hilog私有日志无法输出
12947浏览 • 3回复 待解决
HarmonyOS Resource对象转string报错
716浏览 • 1回复 待解决
HarmonyOS ResourceColor 转16进制 string
1077浏览 • 1回复 待解决
HarmonyOS NAPI层日志输出末尾字符丢失
760浏览 • 1回复 待解决
linux shell利用for语句依次输出
6729浏览 • 1回复 待解决
输出静态库的最佳实践
1127浏览 • 1回复 待解决
提问
该提问已有2人参与 ,帮助了15人