中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
interface回调如何调用
微信扫码分享
@Observed export class TestPlanCommonModel { serviceType: string = '' // toggle是否勾选 isOn: boolean = true constructor(isOn: boolean, serviceType: string) { this.isOn = isOn this.serviceType = serviceType } } @Observed export class TestArray{ testPlanCommonModel: TestPlanCommonModel constructor(testPlanCommonModel: TestPlanCommonModel) { this.testPlanCommonModel = testPlanCommonModel } } @Entry @Component struct Test { @State isOnState: boolean = false; @State listA: TestPlanCommonModel[] = [ new TestPlanCommonModel(true, '1'), new TestPlanCommonModel(true, '2'), new TestPlanCommonModel(false, '3'), new TestPlanCommonModel(false, '1'), new TestPlanCommonModel(false, '1')] // @State listA: boolean[] = [true, true, true, false, false] build() { Column() { Test1({ isOnState: this.isOnState, listA: this.listA }) TestPlanItem({ listA: this.listA }) Button('打印:').onClick(() => { for (let i = 0; i < this.listA.length; i++) { console.log("打印:" + JSON.stringify(this.listA[i])) } }) } } } @Component struct Test1 { @Link isOnState: boolean @Link listA: TestPlanCommonModel[] build() { Column({ space: 10 }) { Toggle({ type: ToggleType.Switch, isOn: true }) .selectedColor('#007DFF') .switchPointColor('#FFFFFF') .onChange((isOn: boolean) => { console.log("点击总开关-修改listA的所有isOn属性为" + isOn) for (let i = 0; i < this.listA.length; i++) { this.listA[i].isOn = isOn } }) Divider() } } } import { MyInterDemo } from '../interface/MyInterDemo' @Entry @Component struct TestInterfacePage { testInter(inter: MyInterDemo) { console.log('testInter'); inter.func_1(); inter.func_2(); inter.func_3('hello'); } build() { Row() { Column() { Button('Test interface') .onClick(() => { let demo: MyInterDemo = { func_1: () => { console.log('func_1'); }, func_2: () => { console.log('func_2'); return true; }, func_3: (arg: string) => { console.log('func_3:' + arg); } } this.testInter(demo); }) } .width('100%') } .height('100%') } } export interface MyInterDemo { func_1: () => void; func_2: () => boolean; func_3: (arg: string) => void; }