赞
踩
- @Entry
- @Component
- struct Index {
- // 使用状态装饰器
- @State message: string = 'Hello Word'
-
- build() {
- Column(){
- Text(this.message)
- }
- };
- }
@Prop | @Link | |
同步类型 | 单向同步 | 双向同步 |
允许装饰的变量类型 |
|
|
初始化方式 | 不允许子组件初始化 | 父子间传递,不允许子组件初始化 |
PS:@Prop定义的数据在子组件不能初始化
- @Entry
- @Component
- struct Index {
- @State msg: string = 'Hello Word'
-
- build() {
- Column() {
- MsgModule({msg:this.msg})
- Button('更改文案')
- .onClick(()=>{
- this.msg = 'Hello arkTs'
- })
- }
- }
- }
-
- @Component
- struct MsgModule {
- @Prop msg:string
- build(){
- Text(this.msg)
- .fontSize(30)
- .fontColor('red')
- }
- }

PS:
- @Entry
- @Component
- struct Index {
- @State msg: string = 'Hello Word'
-
- build() {
- Column() {
- MsgModule({msg: $msg})
- }
- }
- }
-
- @Component
- struct MsgModule {
- @Link msg:string
- build(){
- Row(){
- Text(this.msg)
- .fontSize(30)
- .fontColor('red')
- Button('更改文案')
- .onClick(()=>{
- this.msg = 'Hello arkTs'
- })
- }
- }
- }

@Provide定义的数组,其他组件在使用时候直接使用@Consume定义使用,不需要在调用组件时候进行参数传递
- @Entry
- @Component
- struct Index {
- @Provide msg: string = 'Hello Word'
-
- build() {
- Column() {
- MsgBtnModule()
- }
- }
- }
-
- @Component
- struct MsgBtnModule {
- build(){
- Row(){
- MsgModule()
- }
- }
- }
-
- @Component
- struct MsgModule {
- @Consume msg: string
- build(){
- Row(){
- Text(this.msg)
- .fontSize(30)
- .fontColor('red')
- Button('更改文案')
- .onClick(()=>{
- this.msg = 'Hello arkTs'
- })
- }
- }
- }

- @Observed
- class ArrInt {
- name: string = ""
- price: number = 0
- }
-
- @Entry
- @Component
- struct Index {
- @State num:number = 0
- @State arr: ArrInt[] = [
- {name: '华为 Meta 50',price: 7999},
- {name: '华为 Meta 60',price: 8999},
- {name: '华为 Meta 60 pro',price: 9999},
- ]
-
-
- build() {
- Column() {
- Text('涨价' + this.num.toString() + '次')
- .fontSize(50)
- .margin(20)
- ArrModule({num: $num, arr: $arr})
- }
- }
- }
-
- @Component
- struct ArrModule {
- @Link num: number
- @Link arr: ArrInt[]
-
- build(){
- Row(){
- List({space: 10}){
- ForEach(
- this.arr,
- (item: ArrInt) => {
- ListItem(){
- ArrItemModule({item:item, num: $num})
- }
- }
- )
- }
- }
- }
- }
-
- @Component
- struct ArrItemModule {
- @ObjectLink item: ArrInt
- @Link num: number
-
- build(){
- Column(){
- Text(this.item.name)
- .fontSize(30)
- .fontColor('red')
- Text(this.item.price.toString())
- .fontSize(30)
- .fontColor('#000')
- Button('涨价')
- .onClick(()=>{
- this.num += 1
- })
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。