HarmonyOS new TextAttribute() 会crash,报错TextAttribute is not defined

@Component
export struct IconFontView {
  @Prop content: string | null = null;
  @Prop iconModifier: TextAttribute = new TextAttribute();
 private textModifier: TextModifier = new TextModifier(this.iconModifier);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

以上写法编译可过,运行时会报错

HarmonyOS
2024-12-18 17:34:20
304浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

TextAttribute的用法有误,修改方法如下,请参考文档上用法来写。

文档链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-attribute-modifier-0000001861886665-V5

可参考buttonAttribute的用法:

class MyButtonModifier implements AttributeModifier<ButtonAttribute> {
  isDark: boolean = false
  applyNormalAttribute(instance: ButtonAttribute): void {
    if (this.isDark) {
      instance.backgroundColor(Color.Black)
    } else {
      instance.backgroundColor(Color.Red)
    }
  }
}

@Entry
@Component
struct attributeDemo {
  @State modifier: MyButtonModifier = new MyButtonModifier()

  build() {
    Row() {
      Column() {
        Button("Button")
          .attributeModifier(this.modifier)
          .onClick(() => {
            this.modifier.isDark = !this.modifier.isDark
          })
      }
      .width('100%')
    }
    .height('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.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
分享
微博
QQ
微信
回复
2024-12-18 18:56:08


相关问题
TimePickerFormat is not defined
340浏览 • 1回复 待解决
app启动crash报错Error message:MainPage:
2288浏览 • 1回复 待解决
如何实现crash堆栈抓取、crash回调
2377浏览 • 1回复 待解决
HarmonyOS 关于crash问题
1031浏览 • 1回复 待解决
HarmonyOS taskpool使用crash
2123浏览 • 2回复 待解决
HarmonyOS FrameNode手势检测crash
390浏览 • 1回复 待解决
HarmonyOS RN相关界面crash
470浏览 • 1回复 待解决
HarmonyOS RN ScrollView scrollTo Crash
460浏览 • 1回复 待解决
ArkTs怎么根据模版new对象返回
953浏览 • 0回复 待解决
clazz: new (...args: any[]) => T 无法使用
667浏览 • 1回复 待解决
HarmonyOS 线上crash 等性能监控
517浏览 • 1回复 待解决
HarmonyOS 使用全局 @Builder 应用 crash
457浏览 • 1回复 待解决
HarmonyOS 遇到JSON.stringify crash
828浏览 • 1回复 待解决
HarmonyOS crash异常无法上报问题
667浏览 • 1回复 待解决
new 出来的对象如何获取所属类
2181浏览 • 1回复 待解决
HarmonyOS 唤起自定义弹窗crash
564浏览 • 1回复 待解决
HarmonyOS crash日志如何定位问题代码
1132浏览 • 1回复 待解决
new Date()获取的月和日时间错误。
2598浏览 • 1回复 待解决