马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
ArkUI怎样通过代码动态创建组件(API 9)
办理步伐
ArkUI使用ArkTS声明式开辟范式,开辟者无法持有组件实例,在声明时通过渲染控制语法以及动态构建UI元素的方式,控制组件的创建。
代码示例
- // 条件渲染语句创建组件
- if(this.isTrue) {
- Text("创建文本组件").fontSize(30)
- }
- // 循环渲染语句创建组件
- ForEach(this.nums,(item) => {
- Text(item + '').fontSize(30)
- },item => JSON.stringify(item))
复制代码 使用@Builder装饰器包罗自界说组件的方法与平常方法的区别是什么(API 9)
办理步伐
@Builder装饰的方法中使用了自界说组件,那么该方法每次被调用时,对应的自界说组件均会重新创建,平常方法中不使用@builder装饰,无法容纳自界说组件。
怎样使用@BuilderParam装饰器举行组件传参(API 9)
办理步伐
- 不带参数
对@BuilderParam修饰的属性举行赋值时不带参数(如:content: this.specificParam),则此属性的范例需界说成无返回值的函数(如:@BuilderParam content: () => void)。
- 带参数
对@BuilderParam修饰的属性举行赋值时带参数(如:callContent: this.specificParam1(“111”)),则此属性的范例需界说成any(如:@BuilderParam callContent: any)。
怎样监听数组内对象属性厘革(API 9)
题目征象
数组内存储对象示例,须要对对象的属性厘革举行监听。
办理步伐
通过@Observed共同@ObjectLink装饰符实现。@Observed用于类,@ObjectLink用于变量。
代码示例
- 在类上使用@Observed。
- @Observed
- class ClassA {
- public name: string
- public c: number
- public id: number
- constructor(c: number, name: string = 'OK') {
- this.name = name
- this.c = c
- }
- }
复制代码
- 在组件变量使用@ObjectLink。
- @Component
- struct ViewA {
- label: string = 'ViewA1'
- @ObjectLink a: ClassA
- build() {
- Row() {
- Button(`ViewA [${this.label}] this.a.c= ${this.a.c} +1`)
- .onClick(() => {
- this.a.c += 1
- })
- }.margin({ top: 10 })
- }
- }
复制代码
子组件使用@Link修饰成员变量时,怎样通过父组件传值(API 9)
办理步伐
子组件使用@Link担当父组件的值时,须要使用’$'创建变量之间的引用关系。才气实现同步。
代码示例
@Link语义是从’$'操纵符引出,即$isPlaying是this.isPlaying内部状态的双向数据绑定。当单击子组件PlayButton中的按钮时,@Link变量更改,PlayButton与父组件中的Text和Button将同时举行革新,同样地,当点击父组件中的Button修改this.isPlaying时,子组件PlayButton与父组件中的Text和Button也将同时革新。
- 在父组件使用@State装饰器,通报数据使用$符创建引用。
- @Entry
- @Component
- struct Player {
- @State isPlaying: boolean = false
- build() {
- Column() {
- PlayButton({ buttonPlaying: $isPlaying })
- Text(`Player is ${this.isPlaying ? '' : 'not'} playing`).fontSize(18)
- Button('Parent:' + this.isPlaying)
- .margin(15)
- .onClick(() => {
- this.isPlaying = !this.isPlaying
- })
- }
- }
- }
复制代码
- 在子组件使用@Link担当数据。
- @Component
- struct PlayButton {
- @Link buttonPlaying: boolean
- build() {
- Column() {
- Button(this.buttonPlaying ? 'pause' : 'play')
- .margin(20)
- .onClick(() => {
- this.buttonPlaying = !this.buttonPlaying
- })
- }
- }
- }
复制代码
父组件怎样与孙子组件举行状态同步(API 9)
办理步伐
- 方式一(保举):使用@Provide和@Consume装饰器。在父组件使用@Provide,在孙子组件使用@Consume,可以实现父组件和孙子组件举行双向数据绑定。
- 方式二:使用@State和@Link装饰器。在父组件使用@State,在每一层子组件(子组件和孙子组件)都使用@Link。
代码示例一
- 父组件中使用子组件,通过Provide提供reviewVote参数,供跨级通报给孙子组件。
- @Entry
- @Component
- struct Father{
- @Provide("reviewVote") reviewVotes: number = 0;
- build() {
- Column() {
- Son()
- Button(`Father: ${this.reviewVotes}`)
- ...
- }
- }
- }
复制代码
- 子组件中使用孙组件。
- @Component
- struct Son{
- build() {
- Column() {
- GrandSon()
- }
- }
- }
复制代码
- 孙子组件中使用Consume来担当reviewVote的参数。
- @Component
- struct GrandSon{
- @Consume("reviewVote") reviewVotes: number
- build() {
- Column() {
- Button(`GrandSon: ${this.reviewVotes}`)
- ...
- }.width('100%')
- }
- }
复制代码
代码示例二
- 父组件Father使用@State绑定命据reviewVote。
- @Entry
- @Component
- struct Father {
- @State reviewVotes: number = 0;
- build() {
- Column() {
- Son({reviewVotes:$reviewVotes})
- Button(`Father: ${this.reviewVotes}`)
- ...
- }
- }
- }
复制代码
- 子组件Son中使用@Link担当由父组件Father通报的参数reviewVote。
- @Component
- struct Son{
- @Link reviewVotes: number;
- build() {
- Column() {
- Grandson({reviewVotes:$reviewVotes})
- }
- }
- }
复制代码
- 孙子组件GrandSon使用@Link担当由Son组件通报的参数reviewVote。
- @Component
- struct Grandson{
- @Link reviewVotes: number;
- build() {
- Column() {
- Button(`Grandson: ${this.reviewVotes}`)
- ...
- }.width('100%')
- }
- }
复制代码
Js如
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |