@Entry
@Component
struct App {
@State myText: string = 'HarmonyOS4.0';
build() {
Column() {
Text(`${this.myText}`).fontSize(32).fontWeight(800)
Button('点击').onClick(() => {
this.myText = '欢迎使用鸿蒙'
}).width(100).margin({top:20})
}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}
注意:@Entry装饰的组件,其build函数下的根结点必须为容器组件,并且forEach不能作为根节点。
@Component装饰的组件,其build函数下的根结点可以为非容器组件,并且forEach不能作为根节点。
@Component
struct ExanpleComponent {
@State textValue: string = '组件状态'
build() {
Text(this.textValue)
.fontSize(32)
.fontWeight(600)
.fontColor('#e33')
.onClick(() => {
this.textValue = '组件状态变化'
})
}
}
@Entry
@Component
struct App {
@State myText: string = 'HarmonyOS4.0';
build() {
Column() {
ExanpleComponent()
Text(`${this.myText}`)
.fontSize(32)
.fontWeight(800)
Button('点击')
.width(100)
.margin({ top: 20 })
.onClick(() => {
this.myText = '欢迎使用鸿蒙'
})
}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}
Index.ets @Entry入口文件
Example.ets @Component自定义组件文件
注意:@Style不能有参数。
// 定义全局@style样式
@Styles function publicStyle() {
.width('100%')
.height('100%')
.backgroundColor('#3ce')
}
@Entry
@Component
struct App {
@State myText: string = 'HarmonyOS4.0';
// 定义局部@style样式
@Styles internalStyle(){
.width(200)
.margin({ top: 20 })
}
build() {
Column() {
Button(this.myText)
.internalStyle()
.fontSize(20)
.onClick(() => {
this.myText = '欢迎使用鸿蒙'
})
}.publicStyle().justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}
概念
// 定义全局@Extend样式
@Extend(Column) function publicStyle(width:string,height:string) {
.width(width)
.height(height)
.backgroundColor('#3ae')
}
@Entry
@Component
struct App {
@State myText: string = 'HarmonyOS4.0';
build() {
Column() {
Button(this.myText)
.fontSize(20)
.onClick(() => {
this.myText = '欢迎使用鸿蒙'
})
}.publicStyle('100%','100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}
概述
stateStyles是属性方法,类似于css伪类,ArkUI提供了以下四种状态:
@Styles function focusedStyle() {
.backgroundColor('red')
}
@Entry
@Component
struct App {
@State myText: string = 'HarmonyOS4.0';
@Styles pressedStyle() {
.backgroundColor('green')
}
build() {
Column() {
Button(this.myText)
.fontSize(20)
.stateStyles({
focused: focusedStyle,
pressed: this.pressedStyle,
normal: {
.backgroundColor('blue')
},
})
.onClick(() => {
this.myText = '欢迎使用鸿蒙'
})
}.width('100%').height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}
概念
@Component
struct MyComponent {
@State count: number = 0;
private increaseBy: number = 1;
build() {
}
}
@Component
struct Parent {
build() {
Column() {
// 从父组件初始化,覆盖本地定义的默认值
MyComponent({ count: 1, increaseBy: 2 })
}
}
}
概述
注意:@props装饰器不能在@Entry装饰的自定义组件中使用。
@Component
struct Child {
@Prop count: number;
build() {
Column() {
Text(`子组件: ${this.count} `)
// @Prop装饰的变量不会同步给父组件
Button(`子组件-1`).onClick(() => {
this.count -= 1;
}).margin({ top: 24 })
}
}
}
@Entry
@Component
struct Parent {
@State num: number = 0;
build() {
Column() {
Text(`父组件:${this.num}`)
// 父组件的修改会同步给子组件
Button(`父组件+1`).onClick(() => {
this.num += 1;
}).margin({ top: 24 })
// 子组件
Child({ count: this.num }).margin({ top: 24 })
}.width('100%').justifyContent(FlexAlign.Center).margin({ top: 24 })
}
}
概述
注意:@Link装饰器不能在@Entry装饰的自定义组件中使用。
@Component
struct Child {
@Link items: number[];
build() {
Column() {
Button(`子元素修改数组`).onClick(() => {
this.items = [100, 200, 300];
})
ForEach(this.items, item => {
Text(`${item}`)
})
}
}
}
@Entry
@Component
struct Parent {
@State arr: number[] = [1, 2, 3];
build() {
Column() {
Button(`父元素修改数组`).onClick(() => {
this.arr = [5, 6, 7];
})
ForEach(this.arr,
item => {
Text(`${item}`)
}
)
Child({ items: $arr })
}
}
}
概述
注意:@Provide与@Consume装饰的变量必须通过相同的属性名进行同步数据。
@Component
struct CompD {
// @Consume装饰的变量通过相同的属性名绑定其祖先组件CompA内的@Provide装饰的变量
@Consume publicValue: number;
build() {
Column() {
Text(`D组件${this.publicValue}`)
Button(`D组件按钮-1`)
.onClick(() => this.publicValue -= 1)
}
.width('50%')
}
}
@Component
struct CompC {
build() {
CompD()
}
}
@Component
struct CompB {
build() {
CompC()
}
}
@Entry
@Component
struct CompA {
// @Provide装饰的变量publicValue由入口组件CompA提供其后代组件
@Provide publicValue: number = 0;
build() {
Column() {
Text(`A组件${this.publicValue}`)
Button(`A组件按钮+1`)
.onClick(() => this.publicValue += 1)
CompB()
}
}
}
概述
注意:
- @ObjectLink装饰器不能在@Entry装饰的自定义组件中使用。
- @ObjectLink装饰的变量不能被赋值。
// 开发进行中...
概述
@Entry
@Component
struct CompA {
@Watch('change') @State count: number = 1
change() {
console.log(`状态变化${this.count}`)
}
build() {
Column() {
Button(`点击状态变化${this.count}`)
.onClick(() => {
this.count++
})
}
}
}
概述
注意:LocalStorage创建后,命名属性的类型不可更改
1、@LocalStorageProp —— 单项数据同步
// import { Child } from '../components/Example'
let storage = new LocalStorage({ 'PropA': 47 });
@Component
struct Child {
@LocalStorageProp('PropA') num2: number = 1;
build() {
// 点击后,只改变当前组件显示的num2,不会同步到LocalStorage中
Button(`子组件${this.num2}`)
.onClick(() => {
this.num2 += 1;
})
}
}
@Entry(storage)
@Component
struct CompA {
@LocalStorageProp('PropA') num1: number = 1;
build() {
Column({ space: 15 }) {
Child()
// 点击后,只改变当前组件显示的num1,不会同步到LocalStorage中
Button(`父组件 ${this.num1}`)
.onClick(() => this.num1 += 1)
}
}
}
2、@LocalStorageLink —— 双项数据同步
// import { Child } from '../components/Example'
let storage = new LocalStorage({ 'PropA': 47 });
@Component
struct Child {
@LocalStorageLink('PropA') num2: number = 1;
build() {
Button(`子组件${this.num2}`)
.onClick(() => {
this.num2 += 1;
})
}
}
@Entry(storage)
@Component
struct CompA {
@LocalStorageLink('PropA') num1: number = 1;
build() {
Column({ space: 15 }) {
Child()
Button(`父组件 ${this.num1}`)
.onClick(() => this.num1 += 1)
}
}
}
概述
1、@StorageProp —— 单项数据同步
AppStorage.SetOrCreate('PropA', 47);
@Component
struct Child {
@StorageProp('PropA') num2: number = 1;
build() {
Button(`子组件${this.num2}`)
.onClick(() => {
this.num2 += 1;
})
}
}
@Entry()
@Component
struct CompA {
@StorageProp('PropA') num1: number = 1;
build() {
Column({ space: 20 }) {
Child()
Button(`AppStorage ${this.num1}`)
.onClick(() => this.num1 += 1)
}
}
}
2、@StorageLink —— 双项数据同步
AppStorage.SetOrCreate('PropA', 47);
@Entry()
@Component
struct CompA {
@StorageLink('PropA') storLink: number = 1;
build() {
Column({ space: 20 }) {
Button(`AppStorage ${this.storLink}`)
.onClick(() => this.storLink += 1)
}
}
}
总结:LocalStorage和AppStorage都是运行时内存的状态存储。
概述
PersistentStorage.PersistProp('aProp', 47);
@Entry
@Component
struct Index {
@StorageLink('aProp') num: number = 48
build() {
Row() {
Column() {
// 应用退出时会保存当前结果。重新启动后,会显示上一次的保存结果
Button(`${this.num}`)
.onClick(() => {
this.num += 1;
})
}
}
}
}
概述
概述
@Entry
@Component
struct Parent {
@State simpleList: Array = ['one', 'two', 'three'];
build() {
Row() {
Column() {
ForEach(this.simpleList, // 数据源
(item: string) => {
Text(item).fontSize(32) // 组件生成函数
},
(item: string) => item // 键值生成函数:为数据源arr的每个数组项生成唯一且持久的键值。
)
}
}
}
}
概述
class BasicDataSource implements IDataSource {
private listeners: DataChangeListener[] = []
public totalCount(): number {
return 0
}
public getData(index: number): any {
return undefined
}
registerDataChangeListener(listener: DataChangeListener): void {
if (this.listeners.indexOf(listener) < 0) {
console.info('add listener')
this.listeners.push(listener)
}
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const pos = this.listeners.indexOf(listener);
if (pos >= 0) {
console.info('remove listener')
this.listeners.splice(pos, 1)
}
}
notifyDataReload(): void {
this.listeners.forEach(listener => {
listener.onDataReloaded()
})
}
notifyDataAdd(index: number): void {
this.listeners.forEach(listener => {
listener.onDataAdd(index)
})
}
notifyDataChange(index: number): void {
this.listeners.forEach(listener => {
listener.onDataChange(index)
})
}
notifyDataDelete(index: number): void {
this.listeners.forEach(listener => {
listener.onDataDelete(index)
})
}
notifyDataMove(from: number, to: number): void {
this.listeners.forEach(listener => {
listener.onDataMove(from, to)
})
}
}
class MyDataSource extends BasicDataSource {
// 初始化数据列表
private dataArray: string[] = ['demo1', 'demo2', 'demo3', 'demo4']
public totalCount(): number {
return this.dataArray.length
}
public getData(index: number): any {
return this.dataArray[index]
}
public addData(index: number, data: string): void {
this.dataArray.splice(index, 0, data)
this.notifyDataAdd(index)
}
public pushData(data: string): void {
this.dataArray.push(data)
this.notifyDataAdd(this.dataArray.length - 1)
}
}
@Entry
@Component
struct MyComponent {
private data: MyDataSource = new MyDataSource()
build() {
List({ space: 3 }) {
LazyForEach(this.data, (item: string) => {
ListItem() {
Row() {
Text(item).fontSize(20).margin({ left: 10 })
}.margin({ left: 10, right: 10 })
}
.onClick(() => {
// 每点击一次列表项,数据增加一项
this.data.pushData(`demoVale:${this.data.totalCount()}`)
})
}, item => item)
}.backgroundColor('red')
}
}
概述
1、应用配置
2、入口配置