ArkUI完全支持深层对象的双向同步,可以通过以下两种方案来实现:
组件 | 支持的参数/属性 | 起始API版本 |
|---|---|---|
select | 10 | |
selectAll | 10 | |
selected | 10 | |
selected | 10 | |
selected | 10 | |
mode | 10 | |
checked | 10 | |
rating | 10 | |
value | 10 | |
showSideBar | 10 | |
value | 10 | |
index | 10 | |
index | 10 | |
index | 10 | |
text | 10 | |
text | 10 | |
selected、value | 10 | |
isOn | 10 | |
selected | 10 | |
selected、value | 10 | |
isShow | 10 | |
isShow | 10 | |
refreshing | 8 | |
selected | 10 | |
selected | 10 |
示例代码如下:
@Component
struct SyncPage {
@State model: modelFirst = new modelFirst();
@State message: string = '';
build() {
Column({ space: 20 }) {
Text(this.message);
TextInput({ text: $$this.model.modelThird.modelForth })
.placeholderColor(Color.Grey)
.placeholderFont({ size: 14, weight: 400 })
.caretColor(Color.Blue)
.width(300)
.onChange(() => {
this.message = this.model.modelThird.modelForth;
});
}.width('100%').justifyContent(FlexAlign.Center);
}
} 实体类:
interface ModelInterfaceFir {
modelForth: string;
}
export class modelFirst {
modelFirst: string = '';
modelSecond: number = 0;
modelThird: ModelInterfaceFir = { modelForth: 'modelForth' };
} 示例代码如下:
@ComponentV2
struct LocalChangePage {
@Param model: modelSecond = new modelSecond('modelFirst', 10, new ModelInterfaceSec('modelForth'), new ModelIn(10));
@Event $value: (val: modelSecond) => void = () => {
};
message: string = '';
build() {
Column({ space: 20 }) {
Text(this.model.modelThird.modelForth);
TextInput({ text: this.model.modelThird.modelForth!! })
.placeholderColor(Color.Grey)
.placeholderFont({ size: 14, weight: 400 })
.caretColor(Color.Blue)
.width(300)
.onChange(() => {
this.message = this.model.modelThird.modelForth;
});
};
};
} 实体类:
@ObservedV2
export class ModelInterfaceSec {
@Trace
modelForth: string;
constructor(modelForth: string) {
this.modelForth = modelForth;
}
}
@ObservedV2
export class ModelIn {
@Trace
modelFifth: number;
constructor(modelFifth: number) {
this.modelFifth = modelFifth;
}
}
@ObservedV2
export class modelSecond {
@Trace
modelFirst: string = '';
@Trace
modelSecond: number = 0;
@Trace
modelThird: ModelInterfaceSec = new ModelInterfaceSec('modelForth');
@Trace
modelSixth: ModelIn = new ModelIn(10);
constructor(modelFirst: string, modelSecond: number, modelThird: ModelInterfaceSec, modelSixth: ModelIn) {
this.modelFirst = modelFirst;
this.modelSecond = modelSecond;
this.modelThird = modelThird;
this.modelSixth = modelSixth;
}
} 全量代码如下:
interface ModelInterfaceFir {
modelForth: string;
}
export class modelFirst {
modelFirst: string = '';
modelSecond: number = 0;
modelThird: ModelInterfaceFir = { modelForth: 'modelForth' };
}
@ObservedV2
export class ModelInterfaceSec {
@Trace
modelForth: string;
constructor(modelForth: string) {
this.modelForth = modelForth;
}
}
@ObservedV2
export class ModelIn {
@Trace
modelFifth: number;
constructor(modelFifth: number) {
this.modelFifth = modelFifth;
}
}
@ObservedV2
export class modelSecond {
@Trace
modelFirst: string = '';
@Trace
modelSecond: number = 0;
@Trace
modelThird: ModelInterfaceSec = new ModelInterfaceSec('modelForth');
@Trace
modelSixth: ModelIn = new ModelIn(10);
constructor(modelFirst: string, modelSecond: number, modelThird: ModelInterfaceSec, modelSixth: ModelIn) {
this.modelFirst = modelFirst;
this.modelSecond = modelSecond;
this.modelThird = modelThird;
this.modelSixth = modelSixth;
}
}
@Entry
@ComponentV2
struct LineBreakStrategyExample {
build() {
Column() {
Column({ space: 15 }) {
Text('方案一');
SyncPage();
}
.height('50%')
.margin({
top: 30
});
Column({ space: 15 }) {
Text('方案二');
LocalChangePage();
}
.height('50%');
};
}
}
@ComponentV2
struct LocalChangePage {
@Param model: modelSecond = new modelSecond('modelFirst', 10, new ModelInterfaceSec('modelForth'), new ModelIn(10));
@Event $value: (val: modelSecond) => void = () => {
};
message: string = '';
build() {
Column({ space: 20 }) {
Text(this.model.modelThird.modelForth);
TextInput({ text: this.model.modelThird.modelForth!! })
.placeholderColor(Color.Grey)
.placeholderFont({ size: 14, weight: 400 })
.caretColor(Color.Blue)
.width(300)
.onChange(() => {
this.message = this.model.modelThird.modelForth;
});
};
};
}
@Component
struct SyncPage {
@State model: modelFirst = new modelFirst();
@State message: string = '';
build() {
Column({ space: 20 }) {
Text(this.message);
TextInput({ text: $$this.model.modelThird.modelForth })
.placeholderColor(Color.Grey)
.placeholderFont({ size: 14, weight: 400 })
.caretColor(Color.Blue)
.width(300)
.onChange(() => {
this.message = this.model.modelThird.modelForth;
});
}.width('100%').justifyContent(FlexAlign.Center);
}
}