如何将界面上的状态变量传给服务层,在服务层修改状态变量对象的属性刷新页面

如何将界面上的状态变量传给服务层,在服务层修改状态变量对象的属性刷新页面。

HarmonyOS
2024-06-03 22:11:00
1477浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
wngsheng
class Grade { 
  private _num: number; 
 
  constructor(num: number) { 
    this._num = num 
  } 
 
  get num(): number { 
    return this._num; 
  } 
 
  set num(num: number) { 
    this._num = num; 
  } 
} 
 
 
@Observed 
class Student extends Grade { 
  private _name: string; 
  private _age: number; 
  public static score: string = '100' 
 
  constructor(name: string, age: number, grade: number) { 
    super(grade); 
    this._name = name; 
    this._age = age; 
  } 
 
  get name(): string { 
    return this._name 
  } 
 
  set name(str: string) { 
    this._name = str; 
  } 
 
  get age(): number { 
    return this._age 
  } 
 
  set age(num: number) { 
    this._age = num; 
  } 
} 
 
@Observed 
class StudentList extends Array<Student> { 
  constructor(...list) { 
    super(...list) 
  } 
} 
 
class PageModel { 
  private _list: StudentList; 
 
  constructor() { 
    // 这里采用普通json对象赋值,会导致数据更新时,组件不刷新情况 
    this._list = new StudentList(new Student('张三', 10, 1), new Student('李四', 20, 2)); 
  } 
 
  public get list(): StudentList { 
    return this._list; 
  } 
 
  public set list(list: StudentList) { 
    this._list = list; 
  } 
 
  addList(data: Student) { 
    this._list.push(data) 
  } 
}
@Component 
struct StudentComp { 
  @ObjectLink student: Student; 
 
  build() { 
    Row() { 
      Text(`姓名:${this.student.name};`) 
        .fontSize(20) 
        .margin({ right: 10 }) 
        .onClick(() => { 
          this.student.name += 1; 
        }) 
      Text(`年龄:${this.student.age}`) 
        .fontSize(20) 
        .margin({ right: 10 }) 
        .onClick(() => { 
          this.student.age += 1; 
        }) 
 
      Text(`班级:${this.student.num}`) 
        .fontSize(20) 
        .margin({ right: 10 }) 
        .onClick(() => { 
          this.student.num += 1; 
        }) 
    } 
  } 
} 
 
 
@Component 
struct ListComp { 
  @ObjectLink list: StudentList; 
 
  build() { 
    Column() { 
      ForEach(this.list, (item: Student, index) => { 
        StudentComp({ student: item }) 
      }, item => JSON.stringify(item)) 
    } 
  } 
} 
 
@Entry 
@Component 
struct Index2 { 
  @State pageMode: PageModel = new PageModel(); 
 
  aboutToAppear() { 
 
 
  } 
 
  build() { 
    Column() { 
      Button('add') 
        .onClick(() => { 
          let a = this.pageMode; 
          console.log('---------a------' + JSON.stringify(a)) 
          this.pageMode.list[0].name = '王五' 
          // this.pageMode.addList(new Student('王五', 30)) 
        }) 
      ListComp({ 
        list: this.pageMode.list 
      }) 
    } 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
分享
微博
QQ
微信
回复
2024-06-04 21:38:39


相关问题
HarmonyOS 状态变量刷新问题
1604浏览 • 1回复 待解决
HarmonyOS constraintSize支持状态变量
955浏览 • 1回复 待解决
ArkTS中如何监听状态变量变化?
1999浏览 • 1回复 待解决
状态变量和常规变量有什么区别?
1272浏览 • 2回复 待解决
HarmonyOS taskpool参数是状态变量crash
673浏览 • 1回复 待解决
关于状态变量@state必须知道
2200浏览 • 1回复 待解决