未验证 提交 17c68e5e 编写于 作者: J junyi233 提交者: Gitee

update zh-cn/application-dev/quick-start/arkts-mvvm.md.

Signed-off-by: Njunyi233 <zhengjun29@huawei.com>
上级 645c0b02
...@@ -125,7 +125,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量 ...@@ -125,7 +125,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```ts ```ts
@Component @Component
struct LinkLinkChild { struct LinkLinkChild {
@Link @Watch("testNumChange") testNumGrand: number; @Link @Watch("testNumChange") testNumGrand: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`LinkLinkChild: testNumGrand value ${this.testNumGrand}`); console.log(`LinkLinkChild: testNumGrand value ${this.testNumGrand}`);
...@@ -139,7 +139,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量 ...@@ -139,7 +139,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
@Component @Component
struct PropLinkChild { struct PropLinkChild {
@Prop @Watch("testNumChange") testNumGrand: number; @Prop @Watch("testNumChange") testNumGrand: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`); console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`);
...@@ -175,7 +175,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量 ...@@ -175,7 +175,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```ts ```ts
@Component @Component
struct LinkLinkChild { struct LinkLinkChild {
@Link @Watch("testNumChange") testNumGrand: number; @Link @Watch("testNumChange") testNumGrand: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`LinkLinkChild: testNumGrand value ${this.testNumGrand}`); console.log(`LinkLinkChild: testNumGrand value ${this.testNumGrand}`);
...@@ -189,7 +189,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量 ...@@ -189,7 +189,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
@Component @Component
struct PropLinkChild { struct PropLinkChild {
@Prop @Watch("testNumChange") testNumGrand: number; @Prop @Watch("testNumChange") testNumGrand: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`); console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`);
...@@ -277,7 +277,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量 ...@@ -277,7 +277,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```ts ```ts
@Component @Component
struct LinkLinkChild { struct LinkLinkChild {
@Consume @Watch("testNumChange") testNum: number; @Consume @Watch("testNumChange") testNum: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`LinkLinkChild: testNum value ${this.testNum}`); console.log(`LinkLinkChild: testNum value ${this.testNum}`);
...@@ -290,7 +290,7 @@ struct LinkLinkChild { ...@@ -290,7 +290,7 @@ struct LinkLinkChild {
@Component @Component
struct PropLinkChild { struct PropLinkChild {
@Prop @Watch("testNumChange") testNumGrand: number; @Prop @Watch("testNumChange") testNumGrand: number = 0;
testNumChange(propName: string): void { testNumChange(propName: string): void {
console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`); console.log(`PropLinkChild: testNumGrand value ${this.testNumGrand}`);
...@@ -609,7 +609,7 @@ struct Parent { ...@@ -609,7 +609,7 @@ struct Parent {
```ts ```ts
@Observed class ObservedArray<T> extends Array<T> { @Observed class ObservedArray<T> extends Array<T> {
constructor(args: any[]) { constructor(args: T[]) {
super(...args); super(...args);
} }
/* otherwise empty */ /* otherwise empty */
...@@ -686,10 +686,10 @@ struct ViewB { ...@@ -686,10 +686,10 @@ struct ViewB {
build() { build() {
Column() { Column() {
ForEach(this.arrA, ForEach(this.arrA,
(item) => { (item: ClassA) => {
ViewA({ label: `#${item.id}`, a: item }) ViewA({ label: `#${item.id}`, a: item })
}, },
(item) => item.id.toString() (item: ClassA) => item.id.toString()
) )
Divider().height(10) Divider().height(10)
...@@ -875,9 +875,9 @@ export class Person { ...@@ -875,9 +875,9 @@ export class Person {
```ts ```ts
@Observed @Observed
export class ObservedArray<T> extends Array<T> { export class ObservedArray<T> extends Array<T> {
constructor(args?: any[]) { constructor(args: T[]) {
console.log(`ObservedArray: ${JSON.stringify(args)} `) console.log(`ObservedArray: ${JSON.stringify(args)} `)
if (Array.isArray(args)) { if (args instanceof Array) {
super(...args); super(...args);
} else { } else {
super(args) super(args)
...@@ -916,13 +916,12 @@ export class ObservedArray<T> extends Array<T> { ...@@ -916,13 +916,12 @@ export class ObservedArray<T> extends Array<T> {
Divider().height(8) Divider().height(8)
ForEach(this.contacts, ForEach(this.contacts, (contact: Person) => {
contact => { PersonView({ person: contact, phones: contact.phones as ObservedArray<string>, selectedPerson: this.$selectedPerson })
PersonView({person: contact, phones: contact.phones, selectedPerson: this.$selectedPerson}) },
}, (contact: Person) => contact.id_
contact => contact.id_ )
)
Divider().height(8) Divider().height(8)
Text("Edit:") Text("Edit:")
...@@ -971,7 +970,7 @@ export class ObservedArray<T> extends Array<T> { ...@@ -971,7 +970,7 @@ export class ObservedArray<T> extends Array<T> {
build() { build() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
Text(this.person.name) Text(this.person.name)
if (this.phones.length) { if (this.phones.length > 0) {
Text(this.phones[0]) Text(this.phones[0])
} }
} }
...@@ -1005,12 +1004,12 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1005,12 +1004,12 @@ export class ObservedArray<T> extends Array<T> {
@Link selectedPerson: Person; @Link selectedPerson: Person;
/*在本地副本上编辑,直到点击保存*/ /*在本地副本上编辑,直到点击保存*/
@Prop name: string; @Prop name: string = "";
@Prop address : Address; @Prop address : Address = new Address("", 0, "");
@Prop phones : ObservedArray<string>; @Prop phones : ObservedArray<string> = [];
selectedPersonIndex() : number { selectedPersonIndex() : number {
return this.addrBook.contacts.findIndex((person) => person.id_ == this.selectedPerson.id_); return this.addrBook.contacts.findIndex((person: Person) => person.id_ == this.selectedPerson.id_);
} }
build() { build() {
...@@ -1031,24 +1030,24 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1031,24 +1030,24 @@ export class ObservedArray<T> extends Array<T> {
TextInput({text: this.address.zip.toString()}) TextInput({text: this.address.zip.toString()})
.onChange((value) => { .onChange((value) => {
const result = parseInt(value); const result = Number.parseInt(value);
this.address.zip= isNaN(result) ? 0 : result; this.address.zip= Number.isNaN(result) ? 0 : result;
}) })
if(this.phones.length>0) { if (this.phones.length > 0) {
ForEach(this.phones, ForEach(this.phones,
(phone, index) => { (phone: ResourceStr, index?:number) => {
TextInput({text: phone}) TextInput({ text: phone })
.width(150) .width(150)
.onChange((value) => { .onChange((value) => {
console.log(`${index}. ${value} value has changed`) console.log(`${index}. ${value} value has changed`)
this.phones[index] = value; this.phones[index!] = value;
}) })
}, },
(phone, index) => `${index}-${phone}` (phone: ResourceStr, index?:number) => `${index}-${phone}`
) )
} }
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
Text("Save Changes") Text("Save Changes")
.onClick(() => { .onClick(() => {
...@@ -1103,25 +1102,25 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1103,25 +1102,25 @@ export class ObservedArray<T> extends Array<T> {
// ViewModel classes // ViewModel classes
let nextId = 0; let nextId = 0;
@Observed @Observed
export class ObservedArray<T> extends Array<T> { export class ObservedArray<T> extends Array<T> {
constructor(args?: any[]) { constructor(args: T[]) {
console.log(`ObservedArray: ${JSON.stringify(args)} `) console.log(`ObservedArray: ${JSON.stringify(args)} `)
if (Array.isArray(args)) { if (args instanceof Array) {
super(...args); super(...args);
} else { } else {
super(args) super(args)
} }
} }
} }
@Observed @Observed
export class Address { export class Address {
street: string; street: string;
zip: number; zip: number;
city: string; city: string;
constructor(street: string, constructor(street: string,
zip: number, zip: number,
city: string) { city: string) {
...@@ -1130,14 +1129,14 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1130,14 +1129,14 @@ export class ObservedArray<T> extends Array<T> {
this.city = city; this.city = city;
} }
} }
@Observed @Observed
export class Person { export class Person {
id_: string; id_: string;
name: string; name: string;
address: Address; address: Address;
phones: ObservedArray<string>; phones: ObservedArray<string>;
constructor(name: string, constructor(name: string,
street: string, street: string,
zip: number, zip: number,
...@@ -1154,13 +1153,13 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1154,13 +1153,13 @@ export class ObservedArray<T> extends Array<T> {
export class AddressBook { export class AddressBook {
me: Person; me: Person;
contacts: ObservedArray<Person>; contacts: ObservedArray<Person>;
constructor(me: Person, contacts: Person[]) { constructor(me: Person, contacts: Person[]) {
this.me = me; this.me = me;
this.contacts = new ObservedArray<Person>(contacts); this.contacts = new ObservedArray<Person>(contacts);
} }
} }
//渲染出Person对象的名称和Observed数组<string>中的第一个号码 //渲染出Person对象的名称和Observed数组<string>中的第一个号码
//为了更新电话号码,这里需要@ObjectLink person和@ObjectLink phones, //为了更新电话号码,这里需要@ObjectLink person和@ObjectLink phones,
//不能使用this.person.phones,内部数组的更改不会被观察到。 //不能使用this.person.phones,内部数组的更改不会被观察到。
...@@ -1170,7 +1169,7 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1170,7 +1169,7 @@ export class ObservedArray<T> extends Array<T> {
@ObjectLink person: Person; @ObjectLink person: Person;
@ObjectLink phones: ObservedArray<string>; @ObjectLink phones: ObservedArray<string>;
@Link selectedPerson: Person; @Link selectedPerson: Person;
build() { build() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
Text(this.person.name) Text(this.person.name)
...@@ -1185,26 +1184,26 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1185,26 +1184,26 @@ export class ObservedArray<T> extends Array<T> {
}) })
} }
} }
// 渲染Person的详细信息 // 渲染Person的详细信息
// @Prop装饰的变量从父组件AddressBookView深拷贝数据,将变化保留在本地, TextInput的变化只会在本地副本上进行修改。 // @Prop装饰的变量从父组件AddressBookView深拷贝数据,将变化保留在本地, TextInput的变化只会在本地副本上进行修改。
// 点击 "Save Changes" 会将所有数据的复制通过@Prop到@Link, 同步到其他组件 // 点击 "Save Changes" 会将所有数据的复制通过@Prop到@Link, 同步到其他组件
@Component @Component
struct PersonEditView { struct PersonEditView {
@Consume addrBook: AddressBook; @Consume addrBook: AddressBook;
/* 指向父组件selectedPerson的引用 */ /* 指向父组件selectedPerson的引用 */
@Link selectedPerson: Person; @Link selectedPerson: Person;
/*在本地副本上编辑,直到点击保存*/ /*在本地副本上编辑,直到点击保存*/
@Prop name: string; @Prop name: string = "";
@Prop address: Address; @Prop address: Address = new Address("", 0, "");
@Prop phones: ObservedArray<string>; @Prop phones: ObservedArray<string> = [];
selectedPersonIndex(): number { selectedPersonIndex(): number {
return this.addrBook.contacts.findIndex((person) => person.id_ == this.selectedPerson.id_); return this.addrBook.contacts.findIndex((person: Person) => person.id_ == this.selectedPerson.id_);
} }
build() { build() {
Column() { Column() {
TextInput({ text: this.name }) TextInput({ text: this.name })
...@@ -1215,32 +1214,32 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1215,32 +1214,32 @@ export class ObservedArray<T> extends Array<T> {
.onChange((value) => { .onChange((value) => {
this.address.street = value; this.address.street = value;
}) })
TextInput({ text: this.address.city }) TextInput({ text: this.address.city })
.onChange((value) => { .onChange((value) => {
this.address.city = value; this.address.city = value;
}) })
TextInput({ text: this.address.zip.toString() }) TextInput({ text: this.address.zip.toString() })
.onChange((value) => { .onChange((value) => {
const result = parseInt(value); const result = Number.parseInt(value);
this.address.zip = isNaN(result) ? 0 : result; this.address.zip = Number.isNaN(result) ? 0 : result;
}) })
if (this.phones.length > 0) { if (this.phones.length > 0) {
ForEach(this.phones, ForEach(this.phones,
(phone, index) => { (phone: ResourceStr, index?:number) => {
TextInput({ text: phone }) TextInput({ text: phone })
.width(150) .width(150)
.onChange((value) => { .onChange((value) => {
console.log(`${index}. ${value} value has changed`) console.log(`${index}. ${value} value has changed`)
this.phones[index] = value; this.phones[index!] = value;
}) })
}, },
(phone, index) => `${index}-${phone}` (phone: ResourceStr, index?:number) => `${index}-${phone}`
) )
} }
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) { Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.SpaceBetween }) {
Text("Save Changes") Text("Save Changes")
.onClick(() => { .onClick(() => {
...@@ -1259,49 +1258,48 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1259,49 +1258,48 @@ export class ObservedArray<T> extends Array<T> {
.onClick(() => { .onClick(() => {
let index = this.selectedPersonIndex(); let index = this.selectedPersonIndex();
console.log(`delete contact at index ${index}`); console.log(`delete contact at index ${index}`);
// 删除当前联系人 // 删除当前联系人
this.addrBook.contacts.splice(index, 1); this.addrBook.contacts.splice(index, 1);
// 删除当前selectedPerson,选中态前移一位 // 删除当前selectedPerson,选中态前移一位
index = (index < this.addrBook.contacts.length) ? index : index - 1; index = (index < this.addrBook.contacts.length) ? index : index - 1;
// 如果contract被删除完,则设置me为选中态 // 如果contract被删除完,则设置me为选中态
this.selectedPerson = (index >= 0) ? this.addrBook.contacts[index] : this.addrBook.me; this.selectedPerson = (index >= 0) ? this.addrBook.contacts[index] : this.addrBook.me;
}) })
} }
} }
} }
} }
} }
@Component @Component
struct AddressBookView { struct AddressBookView {
@ObjectLink me: Person; @ObjectLink me: Person;
@ObjectLink contacts: ObservedArray<Person>; @ObjectLink contacts: ObservedArray<Person>;
@State selectedPerson: Person = undefined; @State selectedPerson: Person = new Person("", "", 0, "", []);
aboutToAppear() { aboutToAppear() {
this.selectedPerson = this.me; this.selectedPerson = this.me;
} }
build() { build() {
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) {
Text("Me:") Text("Me:")
PersonView({ person: this.me, phones: this.me.phones, selectedPerson: this.$selectedPerson }) PersonView({ person: this.me, phones: this.me.phones, selectedPerson: this.$selectedPerson })
Divider().height(8) Divider().height(8)
ForEach(this.contacts, ForEach(this.contacts, (contact: Person) => {
contact => { PersonView({ person: contact, phones: contact.phones as ObservedArray<string>, selectedPerson: this.$selectedPerson })
PersonView({ person: contact, phones: contact.phones, selectedPerson: this.$selectedPerson }) },
}, (contact: Person) => contact.id_
contact => contact.id_
) )
Divider().height(8) Divider().height(8)
Text("Edit:") Text("Edit:")
PersonEditView({ PersonEditView({
selectedPerson: this.$selectedPerson, selectedPerson: this.$selectedPerson,
...@@ -1313,7 +1311,7 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1313,7 +1311,7 @@ export class ObservedArray<T> extends Array<T> {
.borderStyle(BorderStyle.Solid).borderWidth(5).borderColor(0xAFEEEE).borderRadius(5) .borderStyle(BorderStyle.Solid).borderWidth(5).borderColor(0xAFEEEE).borderRadius(5)
} }
} }
@Entry @Entry
@Component @Component
struct PageEntry { struct PageEntry {
...@@ -1324,7 +1322,7 @@ export class ObservedArray<T> extends Array<T> { ...@@ -1324,7 +1322,7 @@ export class ObservedArray<T> extends Array<T> {
new Person("Sam", "Itamerenkatu 9", 180, "Helsinki", ["18*********", "18*********"]), new Person("Sam", "Itamerenkatu 9", 180, "Helsinki", ["18*********", "18*********"]),
new Person("Vivi", "Itamerenkatu 9", 180, "Helsinki", ["18*********", "18*********"]), new Person("Vivi", "Itamerenkatu 9", 180, "Helsinki", ["18*********", "18*********"]),
]); ]);
build() { build() {
Column() { Column() {
AddressBookView({ me: this.addrBook.me, contacts: this.addrBook.contacts, selectedPerson: this.addrBook.me }) AddressBookView({ me: this.addrBook.me, contacts: this.addrBook.contacts, selectedPerson: this.addrBook.me })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册