早晚报标题,异步请求导致PaperTitleComponent传参时数据为空,使用provide/consume;consume所在componet不能声明@E…
…ntry,provide与consume名称要完全一致
Showing
2 changed files
with
14 additions
and
11 deletions
| @@ -31,7 +31,8 @@ export struct MorningEveningPaperComponent { | @@ -31,7 +31,8 @@ export struct MorningEveningPaperComponent { | ||
| 31 | // topicId: "10000002068", | 31 | // topicId: "10000002068", |
| 32 | // } | 32 | // } |
| 33 | // } as MorningEveningPaperDTO | 33 | // } as MorningEveningPaperDTO |
| 34 | - @State subTitle: string = '' | 34 | + @Provide title: string = '' |
| 35 | + @Provide subTitle: string = '' | ||
| 35 | 36 | ||
| 36 | async aboutToAppear() { | 37 | async aboutToAppear() { |
| 37 | console.info(TAG, `aboutToAppear`) | 38 | console.info(TAG, `aboutToAppear`) |
| @@ -42,6 +43,7 @@ export struct MorningEveningPaperComponent { | @@ -42,6 +43,7 @@ export struct MorningEveningPaperComponent { | ||
| 42 | try { | 43 | try { |
| 43 | let pageInfoBean = await MorningEveningViewModel.getMorningEveningPageInfo("28927") | 44 | let pageInfoBean = await MorningEveningViewModel.getMorningEveningPageInfo("28927") |
| 44 | this.pageInfoBean = pageInfoBean; | 45 | this.pageInfoBean = pageInfoBean; |
| 46 | + this.title = this.pageInfoBean?.topicInfo?.title | ||
| 45 | let dateTime = DateTimeUtils.parseDate(this.pageInfoBean?.topicInfo?.topicDate ?? '', DateTimeUtils.PATTERN_DATE_HYPHEN); | 47 | let dateTime = DateTimeUtils.parseDate(this.pageInfoBean?.topicInfo?.topicDate ?? '', DateTimeUtils.PATTERN_DATE_HYPHEN); |
| 46 | this.subTitle = DateTimeUtils.formatDate(dateTime, PATTERN_DATE_CN_RN) | 48 | this.subTitle = DateTimeUtils.formatDate(dateTime, PATTERN_DATE_CN_RN) |
| 47 | Logger.info(TAG, "pageInfoBean topicDate = " + this.pageInfoBean?.topicInfo?.topicDate) | 49 | Logger.info(TAG, "pageInfoBean topicDate = " + this.pageInfoBean?.topicInfo?.topicDate) |
| @@ -80,12 +82,13 @@ export struct MorningEveningPaperComponent { | @@ -80,12 +82,13 @@ export struct MorningEveningPaperComponent { | ||
| 80 | .height('100%') | 82 | .height('100%') |
| 81 | .margin({ left: 14, right: 14 }) | 83 | .margin({ left: 14, right: 14 }) |
| 82 | 84 | ||
| 83 | - PaperTitleComponent({ | ||
| 84 | - // title: this.morningEveningPaperDTO?.topicInfo?.title ?? "", | ||
| 85 | - title: this.pageInfoBean?.topicInfo?.title ?? "", | ||
| 86 | - // subtitle: this.morningEveningPaperDTO?.topicInfo?.topicDate ?? '' | ||
| 87 | - subtitle: this.subTitle | ||
| 88 | - }) | 85 | + PaperTitleComponent() |
| 86 | + // PaperTitleComponent({ | ||
| 87 | + // // title: this.morningEveningPaperDTO?.topicInfo?.title ?? "", | ||
| 88 | + // title: this.title ?? "", | ||
| 89 | + // // subtitle: this.morningEveningPaperDTO?.topicInfo?.topicDate ?? '' | ||
| 90 | + // subtitle: this.subTitle | ||
| 91 | + // }) | ||
| 89 | .margin({ left: 14, right: 14 }) | 92 | .margin({ left: 14, right: 14 }) |
| 90 | } | 93 | } |
| 91 | .width('100%') | 94 | .width('100%') |
| @@ -3,11 +3,11 @@ import router from '@ohos.router'; | @@ -3,11 +3,11 @@ import router from '@ohos.router'; | ||
| 3 | /** | 3 | /** |
| 4 | * 早晚报页面标题bar | 4 | * 早晚报页面标题bar |
| 5 | */ | 5 | */ |
| 6 | -@Entry | 6 | +// @Entry |
| 7 | @Component | 7 | @Component |
| 8 | export struct PaperTitleComponent { | 8 | export struct PaperTitleComponent { |
| 9 | - @State title?: string = 'title' | ||
| 10 | - @State subtitle?: string = 'subtitle' | 9 | + @Consume title?: string |
| 10 | + @Consume subTitle?: string | ||
| 11 | 11 | ||
| 12 | aboutToAppear() { | 12 | aboutToAppear() { |
| 13 | } | 13 | } |
| @@ -31,7 +31,7 @@ export struct PaperTitleComponent { | @@ -31,7 +31,7 @@ export struct PaperTitleComponent { | ||
| 31 | .fontColor($r('app.color.white')) | 31 | .fontColor($r('app.color.white')) |
| 32 | .maxLines(1) | 32 | .maxLines(1) |
| 33 | 33 | ||
| 34 | - Text(this.subtitle ?? '')// Text('2024年\n1月16日') | 34 | + Text(this.subTitle ?? '')// Text('2024年\n1月16日') |
| 35 | // .width(50) | 35 | // .width(50) |
| 36 | .margin({ left: 5 }) | 36 | .margin({ left: 5 }) |
| 37 | .fontSize(12) | 37 | .fontSize(12) |
-
Please register or login to post a comment