HarmonyOS鸿蒙Next中如何实现两个$r资源引用字符串的拼接,例如Text($r('app.string.xxx') + $r('app.string.yyy))
HarmonyOS鸿蒙Next中如何实现两个$r资源引用字符串的拼接,例如Text($r(‘app.string.xxx’) + $r('app.string.yyy))
Text(${$r('app.string.permission_net')}哈哈哈$r('app.string.permission_net')
)
更多关于HarmonyOS鸿蒙Next中如何实现两个$r资源引用字符串的拼接,例如Text($r('app.string.xxx') + $r('app.string.yyy))的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
代码跑起来不太行,
找HarmonyOS工作还需要会Flutter技术的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17
Text() { Span($r(‘app.string.app_name’)) Span($r(‘app.string.module_desc’)) }
在HarmonyOS Next中拼接$r资源字符串,可使用ArkTS的字符串模板:
Text(`${$r('app.string.xxx')}${$r('app.string.yyy')}`)
或使用字符串连接方法:
Text($r('app.string.xxx').concat($r('app.string.yyy')))
这两种方式都直接操作ArkUI的资源引用结果。注意$r返回的是Resource类型对象,自动转换为字符串时会调用其toString方法。
在HarmonyOS Next中,直接使用$r('app.string.xxx') + $r('app.string.yyy')
进行字符串拼接是不支持的。$r
是资源引用语法,返回的是Resource
对象而非字符串。
正确实现方式有两种:
- 使用字符串模板:
Text(`${$r('app.string.xxx').value}${$r('app.string.yyy').value}`)
- 先获取字符串值再拼接:
let str1 = $r('app.string.xxx').value
let str2 = $r('app.string.yyy').value
Text(str1 + str2)
这两种方式都是先通过.value
获取实际的字符串值,然后再进行拼接操作。注意$r().value
可能会返回undefined
,建议做好空值判断。