裝飾器使用說(shuō)明
- 當(dāng)前@Styles僅支持通用屬性和通用事件。
- @Styles方法不支持參數(shù)
- @Styles可以定義在組件內(nèi)或全局卸勺,在全局定義時(shí)需在方法名前面添加function關(guān)鍵字,組件內(nèi)定義時(shí)則不需要添加function關(guān)鍵字。
只能在當(dāng)前文件內(nèi)使用,不支持export。
使用場(chǎng)景
// 定義在全局的@Styles封裝的樣式
@Styles function globalFancy () {
.width(150)
.height(100)
.backgroundColor(Color.Pink)
}
@Entry
@Component
struct FancyUse {
@State heightValue: number = 100
// 定義在組件內(nèi)的@Styles封裝的樣式
@Styles fancy() {
.width(200)
.height(this.heightValue)
.backgroundColor(Color.Yellow)
.onClick(() => {
this.heightValue = 200
})
}
build() {
Column({ space: 10 }) {
// 使用全局的@Styles封裝的樣式
Text('FancyA')
.globalFancy()
.fontSize(30)
// 使用組件內(nèi)的@Styles封裝的樣式
Text('FancyB')
.fancy()
.fontSize(30)
}
}
}