Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
17c68e5e
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
17c68e5e
编写于
9月 04, 2023
作者:
J
junyi233
提交者:
Gitee
9月 04, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update zh-cn/application-dev/quick-start/arkts-mvvm.md.
Signed-off-by:
N
junyi233
<
zhengjun29@huawei.com
>
上级
645c0b02
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
83 addition
and
85 deletion
+83
-85
zh-cn/application-dev/quick-start/arkts-mvvm.md
zh-cn/application-dev/quick-start/arkts-mvvm.md
+83
-85
未找到文件。
zh-cn/application-dev/quick-start/arkts-mvvm.md
浏览文件 @
17c68e5e
...
...
@@ -125,7 +125,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```
ts
@
Component
struct
LinkLinkChild
{
@
Link
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
;
@
Link
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`LinkLinkChild: testNumGrand value
${
this
.
testNumGrand
}
`
);
...
...
@@ -139,7 +139,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
@
Component
struct
PropLinkChild
{
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
;
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`PropLinkChild: testNumGrand value
${
this
.
testNumGrand
}
`
);
...
...
@@ -175,7 +175,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```
ts
@
Component
struct
LinkLinkChild
{
@
Link
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
;
@
Link
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`LinkLinkChild: testNumGrand value
${
this
.
testNumGrand
}
`
);
...
...
@@ -189,7 +189,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
@
Component
struct
PropLinkChild
{
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
;
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`PropLinkChild: testNumGrand value
${
this
.
testNumGrand
}
`
);
...
...
@@ -277,7 +277,7 @@ ViewModel通常包含多个顶层数据源。\@State和\@Provide装饰的变量
```
ts
@
Component
struct
LinkLinkChild
{
@
Consume
@
Watch
(
"
testNumChange
"
)
testNum
:
number
;
@
Consume
@
Watch
(
"
testNumChange
"
)
testNum
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`LinkLinkChild: testNum value
${
this
.
testNum
}
`
);
...
...
@@ -290,7 +290,7 @@ struct LinkLinkChild {
@
Component
struct
PropLinkChild
{
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
;
@
Prop
@
Watch
(
"
testNumChange
"
)
testNumGrand
:
number
=
0
;
testNumChange
(
propName
:
string
):
void
{
console
.
log
(
`PropLinkChild: testNumGrand value
${
this
.
testNumGrand
}
`
);
...
...
@@ -609,7 +609,7 @@ struct Parent {
```
ts
@
Observed
class
ObservedArray
<
T
>
extends
Array
<
T
>
{
constructor
(
args
:
any
[])
{
constructor
(
args
:
T
[])
{
super
(...
args
);
}
/* otherwise empty */
...
...
@@ -686,10 +686,10 @@ struct ViewB {
build
()
{
Column
()
{
ForEach
(
this
.
arrA
,
(
item
)
=>
{
(
item
:
ClassA
)
=>
{
ViewA
({
label
:
`#
${
item
.
id
}
`
,
a
:
item
})
},
(
item
)
=>
item
.
id
.
toString
()
(
item
:
ClassA
)
=>
item
.
id
.
toString
()
)
Divider
().
height
(
10
)
...
...
@@ -875,9 +875,9 @@ export class Person {
```
ts
@
Observed
export
class
ObservedArray
<
T
>
extends
Array
<
T
>
{
constructor
(
args
?:
any
[])
{
constructor
(
args
:
T
[])
{
console
.
log
(
`ObservedArray:
${
JSON
.
stringify
(
args
)}
`
)
if
(
Array
.
isArray
(
args
)
)
{
if
(
args
instanceof
Array
)
{
super
(...
args
);
}
else
{
super
(
args
)
...
...
@@ -916,11 +916,10 @@ export class ObservedArray<T> extends Array<T> {
Divider
().
height
(
8
)
ForEach
(
this
.
contacts
,
contact
=>
{
PersonView
({
person
:
contact
,
phones
:
contact
.
phones
,
selectedPerson
:
this
.
$selectedPerson
})
ForEach
(
this
.
contacts
,
(
contact
:
Person
)
=>
{
PersonView
({
person
:
contact
,
phones
:
contact
.
phones
as
ObservedArray
<
string
>
,
selectedPerson
:
this
.
$selectedPerson
})
},
contact
=>
contact
.
id_
(
contact
:
Person
)
=>
contact
.
id_
)
Divider
().
height
(
8
)
...
...
@@ -971,7 +970,7 @@ export class ObservedArray<T> extends Array<T> {
build
()
{
Flex
({
direction
:
FlexDirection
.
Row
,
justifyContent
:
FlexAlign
.
SpaceBetween
})
{
Text
(
this
.
person
.
name
)
if
(
this
.
phones
.
length
)
{
if
(
this
.
phones
.
length
>
0
)
{
Text
(
this
.
phones
[
0
])
}
}
...
...
@@ -1005,12 +1004,12 @@ export class ObservedArray<T> extends Array<T> {
@Link selectedPerson: Person;
/*在本地副本上编辑,直到点击保存*/
@Prop name: string;
@Prop address : Address;
@Prop phones : ObservedArray<string>;
@Prop name: string
= ""
;
@Prop address : Address
= new Address("", 0, "")
;
@Prop phones : ObservedArray<string>
= []
;
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() {
...
...
@@ -1031,21 +1030,21 @@ export class ObservedArray<T> extends Array<T> {
TextInput({text: this.address.zip.toString()})
.onChange((value) => {
const result = parseInt(value);
this.address.zip= isNaN(result) ? 0 : result;
const result =
Number.
parseInt(value);
this.address.zip=
Number.
isNaN(result) ? 0 : result;
})
if
(this.phones.length>
0) {
if
(this.phones.length >
0) {
ForEach(this.phones,
(phone, index
) => {
TextInput({text: phone
})
(phone: ResourceStr, index?:number
) => {
TextInput({ text: phone
})
.width(150)
.onChange((value) => {
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}
`
)
}
...
...
@@ -1106,9 +1105,9 @@ export class ObservedArray<T> extends Array<T> {
@Observed
export class ObservedArray<T> extends Array<T> {
constructor(args
?: any
[]) {
constructor(args
: T
[]) {
console.log(`
ObservedArray: ${JSON.stringify(args)}
`)
if (
Array.isArray(args)
) {
if (
args instanceof Array
) {
super(...args);
} else {
super(args)
...
...
@@ -1197,12 +1196,12 @@ export class ObservedArray<T> extends Array<T> {
@Link selectedPerson: Person;
/*在本地副本上编辑,直到点击保存*/
@Prop name: string;
@Prop address: Address;
@Prop phones: ObservedArray<string>;
@Prop name: string
= ""
;
@Prop address: Address
= new Address("", 0, "")
;
@Prop phones: ObservedArray<string>
= []
;
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() {
...
...
@@ -1223,21 +1222,21 @@ export class ObservedArray<T> extends Array<T> {
TextInput({ text: this.address.zip.toString() })
.onChange((value) => {
const result = parseInt(value);
this.address.zip = isNaN(result) ? 0 : result;
const result =
Number.
parseInt(value);
this.address.zip =
Number.
isNaN(result) ? 0 : result;
})
if (this.phones.length > 0) {
ForEach(this.phones,
(phone
, index
) => {
(phone
: ResourceStr, index?:number
) => {
TextInput({ text: phone })
.width(150)
.onChange((value) => {
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}
`
)
}
...
...
@@ -1280,7 +1279,7 @@ export class ObservedArray<T> extends Array<T> {
struct AddressBookView {
@ObjectLink me: Person;
@ObjectLink contacts: ObservedArray<Person>;
@State selectedPerson: Person =
undefined
;
@State selectedPerson: Person =
new Person("", "", 0, "", [])
;
aboutToAppear() {
this.selectedPerson = this.me;
...
...
@@ -1293,11 +1292,10 @@ export class ObservedArray<T> extends Array<T> {
Divider().height(8)
ForEach(this.contacts,
contact => {
PersonView({ person: contact, phones: contact.phones, selectedPerson: this.$selectedPerson })
ForEach(this.contacts, (contact: Person) => {
PersonView({ person: contact, phones: contact.phones as ObservedArray<string>, selectedPerson: this.$selectedPerson })
},
contact
=> contact.id_
(contact: Person)
=> contact.id_
)
Divider().height(8)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录