目前@Styles不支持跨文件使用,如果需要跨文件使用样式,可使用attributeModifier属性。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-attribute-modifier-0000001815767740\#ZH-CN\_TOPIC\_0000001815767740\_\_attributemodifierdemo代码:styles.ets export class MyTextModifier implements AttributeModifier { isDark: boolean = false applyNormalAttribute(instance: TextAttribute): void { if (this.isDark) { instance.backgroundColor(Color.Black) } else { instance.backgroundColor(Color.Red) } } } XXX.ets import {MyTextModifier} from '../model/styles' @Entry @Component export struct Faq_240412170839093Page { @State message: string = 'Hello World'; @State modifier: MyTextModifier = new MyTextModifier() build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .attributeModifier(this.modifier) } .width('100%') } .height('100%') } }
目前@Styles不支持跨文件使用,如果需要跨文件使用样式,可使用attributeModifier属性。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-universal-attributes-attribute-modifier-0000001815767740\#ZH-CN\_TOPIC\_0000001815767740\_\_attributemodifier
demo代码: