UI控件上的字符需要动态变化,字符是需要国际化的,因此存储在资源目录下的string文件中
动态变化的字符串是由当前页面的变量组合而成(例如发送短信验证码的倒计时数字会持续变化)
State装饰的状态变量在API11中可以定义联合类型的变量(string| Resource)动态适配,但是在API9则无法定义State装饰的联合变量,还不支持
因此需要把Resource的string内容转换为string类型
API9使用stage应用模型,通过context获取resourceManager,然后调用getStringValue,把资源目录下的string转变为string类型
getStringFromResource(source: Resource) {
let context = getContext(this) as common.UIAbilityContext
context.resourceManager.getStringValue(source.id, (error, value) => {
if (error != null) {
} else {
this.smsBtnContent = value
}
})
}
//调用
this.getStringFromResource($r('app.string.reSendSMS'))