未验证 提交 993c7c34 编写于 作者: 1 189******51 提交者: Gitee

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

Signed-off-by: N189******51 <lipeicheng5@huawei.com>
上级 28458a5f
...@@ -322,71 +322,71 @@ Child自定义组件中的变化: ...@@ -322,71 +322,71 @@ Child自定义组件中的变化:
1. playCountLink的刷新会同步回LocalStorage,并且引起兄弟组件和父组件相应的刷新。 1. playCountLink的刷新会同步回LocalStorage,并且引起兄弟组件和父组件相应的刷新。
```ts ```ts
class Data { class Data {
countStorage: number = 0; countStorage: number = 0;
} }
let data: Data = { countStorage: 1 } let data: Data = { countStorage: 1 }
let storage = new LocalStorage(data); let storage = new LocalStorage(data);
@Component @Component
struct Child { struct Child {
// 子组件实例的名字 // 子组件实例的名字
label: string = 'no name'; label: string = 'no name';
// 和LocalStorage中“countStorage”的双向绑定数据 // 和LocalStorage中“countStorage”的双向绑定数据
@LocalStorageLink('countStorage') playCountLink: number = 0; @LocalStorageLink('countStorage') playCountLink: number = 0;
build() { build() {
Row() { Row() {
Text(this.label) Text(this.label)
.width(50).height(60).fontSize(12) .width(50).height(60).fontSize(12)
Text(`playCountLink ${this.playCountLink}: inc by 1`) Text(`playCountLink ${this.playCountLink}: inc by 1`)
.onClick(() => { .onClick(() => {
this.playCountLink += 1; this.playCountLink += 1;
}) })
.width(200).height(60).fontSize(12) .width(200).height(60).fontSize(12)
}.width(300).height(60) }.width(300).height(60)
} }
} }
@Entry(storage) @Entry(storage)
@Component @Component
struct Parent { struct Parent {
@LocalStorageLink('countStorage') playCount: number = 0; @LocalStorageLink('countStorage') playCount: number = 0;
build() { build() {
Column() { Column() {
Row() { Row() {
Text('Parent') Text('Parent')
.width(50).height(60).fontSize(12) .width(50).height(60).fontSize(12)
Text(`playCount ${this.playCount} dec by 1`) Text(`playCount ${this.playCount} dec by 1`)
.onClick(() => { .onClick(() => {
this.playCount -= 1; this.playCount -= 1;
}) })
.width(250).height(60).fontSize(12) .width(250).height(60).fontSize(12)
}.width(300).height(60) }.width(300).height(60)
Row() { Row() {
Text('LocalStorage') Text('LocalStorage')
.width(50).height(60).fontSize(12) .width(50).height(60).fontSize(12)
Text(`countStorage ${this.playCount} incr by 1`) Text(`countStorage ${this.playCount} incr by 1`)
.onClick(() => { .onClick(() => {
let countStorage: number | undefined = storage.get<number>('countStorage'); let countStorage: number | undefined = storage.get<number>('countStorage');
if (countStorage != undefined){ if (countStorage != undefined){
countStorage += 1; countStorage += 1;
storage.set<number>('countStorage', countStorage); storage.set<number>('countStorage', countStorage);
} }
}) })
.width(250).height(60).fontSize(12) .width(250).height(60).fontSize(12)
}.width(300).height(60) }.width(300).height(60)
Child({ label: 'ChildA' }) Child({ label: 'ChildA' })
Child({ label: 'ChildB' }) Child({ label: 'ChildB' })
Text(`playCount in LocalStorage for debug ${storage.get<number>('countStorage')}`) Text(`playCount in LocalStorage for debug ${storage.get<number>('countStorage')}`)
.width(300).height(60).fontSize(12) .width(300).height(60).fontSize(12)
} }
} }
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册