From 781901a8facadb9874e0555a7c9043f65b26da9d Mon Sep 17 00:00:00 2001 From: zhuzijia Date: Mon, 28 Aug 2023 11:24:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4appstorage=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzijia Change-Id: Id44c56e39acce9d5c3a8da536c395391255b81fc --- .../quick-start/arkts-appstorage.md | 175 ------------------ 1 file changed, 175 deletions(-) diff --git a/zh-cn/application-dev/quick-start/arkts-appstorage.md b/zh-cn/application-dev/quick-start/arkts-appstorage.md index 40e9ce2304..4b81dea491 100644 --- a/zh-cn/application-dev/quick-start/arkts-appstorage.md +++ b/zh-cn/application-dev/quick-start/arkts-appstorage.md @@ -297,181 +297,6 @@ export struct TapImage { 以下示例为消息机制方式订阅事件,会导致回调监听的节点数较多,非常耗时,不推荐以此来实现应用代码。 -```ts -// xxx.ets -class ViewData { - title: string; - uri: Resource; - color: Color = Color.Black; - - constructor(title: string, uri: Resource) { - this.title = title; - this.uri = uri - } -} - -@Entry -@Component -struct Gallery2 { - dataList: Array = [new ViewData('flower', $r('app.media.icon')), new ViewData('OMG', $r('app.media.icon')), new ViewData('OMG', $r('app.media.icon'))] - scroller: Scroller = new Scroller() - - build() { - Column() { - Grid(this.scroller) { - ForEach(this.dataList, (item: ViewData, index?: number) => { - GridItem() { - TapImage({ - uri: item.uri, - index: index - }) - }.aspectRatio(1) - - }, (item: ViewData, index?: number) => { - return JSON.stringify(item) + index; - }) - }.columnsTemplate('1fr 1fr') - } - - } -} - -@Component -export struct TapImage { - @StorageLink('tapIndex') @Watch('onTapIndexChange') tapIndex: number = -1; - @State tapColor: Color = Color.Black; - private index: number; - private uri: Resource; - - // 判断是否被选中 - onTapIndexChange() { - if (this.tapIndex >= 0 && this.index === this.tapIndex) { - console.info(`tapindex: ${this.tapIndex}, index: ${this.index}, red`) - this.tapColor = Color.Red; - } else { - console.info(`tapindex: ${this.tapIndex}, index: ${this.index}, black`) - this.tapColor = Color.Black; - } - } - - build() { - Column() { - Image(this.uri) - .objectFit(ImageFit.Cover) - .onClick(() => { - this.tapIndex = this.index; - }) - .border({ width: 5, style: BorderStyle.Dotted, color: this.tapColor }) - } - - } -} -``` - -### 以持久化方式订阅某个事件并接收事件回调 - -推荐使用持久化方式订阅某个事件并接收事件回调,可以减少开销,增强代码的可读性。 - - -```ts -// xxx.ets -import emitter from '@ohos.events.emitter'; - -let NextID: number = 0; - -class ViewData { - title: string; - uri: Resource; - color: Color = Color.Black; - id: number; - - constructor(title: string, uri: Resource) { - this.title = title; - this.uri = uri - this.id = NextID++; - } -} - -@Entry -@Component -struct Gallery2 { - dataList: Array = [new ViewData('flower', $r('app.media.icon')), new ViewData('OMG', $r('app.media.icon')), new ViewData('OMG', $r('app.media.icon'))] - scroller: Scroller = new Scroller() - private preIndex: number = -1 - - build() { - Column() { - Grid(this.scroller) { - ForEach(this.dataList, (item: ViewData) => { - GridItem() { - TapImage({ - uri: item.uri, - index: item.id - }) - }.aspectRatio(1) - .onClick(() => { - if (this.preIndex === item.id) { - return - } - var innerEvent = { eventId: item.id } - // 选中态:黑变红 - var eventData = { - data: { - "colorTag": 1 - } - } - emitter.emit(innerEvent, eventData) - - if (this.preIndex != -1) { - console.info(`preIndex: ${this.preIndex}, index: ${item.id}, black`) - var innerEvent = { eventId: this.preIndex } - // 取消选中态:红变黑 - var eventData = { - data: { - "colorTag": 0 - } - } - emitter.emit(innerEvent, eventData) - } - this.preIndex = item.id - }) - - }, (item: ViewData) => JSON.stringify(item)) - }.columnsTemplate('1fr 1fr') - } - - } -} - -@Component -export struct TapImage { - @State tapColor: Color = Color.Black; - private index: number; - private uri: Resource; - - onTapIndexChange(colorTag: emitter.EventData) { - this.tapColor = colorTag.data.colorTag ? Color.Red : Color.Black - } - - aboutToAppear() { - //定义事件ID - var innerEvent = { eventId: this.index } - emitter.on(innerEvent, this.onTapIndexChange.bind(this)) - } - - build() { - Column() { - Image(this.uri) - .objectFit(ImageFit.Cover) - .border({ width: 5, style: BorderStyle.Dotted, color: this.tapColor }) - } - } -} -``` - -以下示例为消息机制方式订阅事件,会导致回调监听的节点数较多,非常耗时,不推荐以此来实现应用代码。 - - ```ts // xxx.ets class ViewData { -- GitLab