From 00eec76ffa550c94b2724ee03633c3511b8e98b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Thu, 8 May 2025 19:07:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/entryability/EntryAbility.ets | 9 ++++++++- entry/src/main/ets/pages/Index.ets | 3 ++- entry/src/main/ets/view/ListPage.ets | 3 ++- entry/src/main/ets/view/TabBar.ets | 7 ++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 0783712..64f2a18 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -43,7 +43,14 @@ export default class EntryAbility extends UIAbility { } onWindowStageCreate(windowStage: window.WindowStage): void { - windowStage.loadContent('pages/Index', this.storage); + windowStage.loadContent('pages/Index', this.storage, (err, data) => { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + }); } async getCache(): Promise { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index ea872e9..cb4e958 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,7 +16,8 @@ import { FULL_HEIGHT, FULL_WIDTH } from '../common/constant/CommonConstants'; import TabBar from '../view/TabBar'; -let storage = LocalStorage.getShared(); +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +const storage = uiContext?.getSharedLocalStorage() @Entry(storage) @Component diff --git a/entry/src/main/ets/view/ListPage.ets b/entry/src/main/ets/view/ListPage.ets index 9881bd8..815f9f4 100644 --- a/entry/src/main/ets/view/ListPage.ets +++ b/entry/src/main/ets/view/ListPage.ets @@ -18,6 +18,7 @@ import Item from './NewsItem'; import ListModel from '../model/ListModel'; import { ResData, Refresh } from '../common/types/CommonTypes'; import { getUrl, loadMore, refreshing, refreshState } from '../common/utils/Refresh'; +import { common } from '@kit.AbilityKit'; @Component export default struct ListPage { @@ -25,7 +26,7 @@ export default struct ListPage { @State newsModel: ListModel = new ListModel(); @Watch('getInitData') @LocalStorageLink('List') List: ResData[] = []; @LocalStorageLink('Fail') isFail: boolean = false; - context = getContext(); + context = this.getUIContext().getHostContext() as common.UIAbilityContext; getInitData() { this.newsModel.currentPage = 1; diff --git a/entry/src/main/ets/view/TabBar.ets b/entry/src/main/ets/view/TabBar.ets index 1e34066..28c901d 100644 --- a/entry/src/main/ets/view/TabBar.ets +++ b/entry/src/main/ets/view/TabBar.ets @@ -13,11 +13,11 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; import { DataCache } from '@hadss/datacache'; import ListPage from './ListPage'; import * as Const from '../common/constant/CommonConstants'; import { TabType } from '../common/types/CommonTypes'; +import { common } from '@kit.AbilityKit'; @Component export default struct TabBar { @@ -25,6 +25,7 @@ export default struct TabBar { @State currentIndex: number = 0; @State currentPage: number = 1; private controller: TabsController = new TabsController(); + context = this.getUIContext().getHostContext() as common.UIAbilityContext; build() { Column() { @@ -52,8 +53,8 @@ export default struct TabBar { .fontSize(Const.CLEAR_FONT_SIZE) .fontWeight(Const.CLEAR_FONT_WEIGHT) .onClick(() => { - DataCache.getInstance(getContext(this)).clear().then(() => { - promptAction.showToast({ + DataCache.getInstance(this.context).clear().then(() => { + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.clear_success'), duration: Const.ANIMATION_DURATION }); -- Gitee