HarmonyOS 如何watch viewmodel中的数据变化显示对话框

viewmodel模式有个对话框根据请求网络还是请求缓存来决定是否显示,所有这个loadingPageStatus需要放到viewmodel中。但是如何watch他的变化来显示对话框,代码如下:

import { NavigationManager } from '@cmb/router';
import hilog from '@ohos.hilog';

@Component
export struct Page {
  build() {
    NavDestination() {
      RelativeContainer() {
        Button('跳转至测试页')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          })
      }
      .height('100%')
      .width('100%')
      .onClick(() => {
        NavigationManager.getNavPathStack().pushDestinationByName('web', undefined)
      })
    }
    .onAppear(() => {
      hilog.debug(1, "NavigationTest", "onAppear0");
    })
    .onDisAppear(() => {
      hilog.debug(1, "NavigationTest", "onDisAppear0");
    })
    .onShown(() => {
      hilog.debug(1, "NavigationTest", "onShow0");
    })
    .onHidden(() => {
      hilog.debug(1, "NavigationTest", "onHidden0");
    })
    .title("首页")
  }
}

@Entry
@Component
struct Index {
  @State message: string = '测试Navigation生命周期';

  aboutToAppear() {
    let modules: string[] = ["@cmb/module1"]
    modules.forEach(async (value: string, index: number, array: string[]) => {
      let ns: ESObject = await import(value);
      ns.dynamicImport("");
    })
  }

  @Builder
  routerMap(builderName: string, param: object) {
    // Obtain the WrappedBuilder object based on the module name, create a page through the builder interface, and import the param parameter.
    NavigationManager.getBuilder(builderName).builder(param);
  };

  build() {
    Navigation(NavigationManager.getNavPathStack()) {
      NavDestination() {
        Page().width("100%").height("100%")
      }
      .onShown(() => {
        console.log("首页onShow")
      })
      .onHidden(() => {
        console.log("首页onHidden")
      })
      .onAppear(() => {
        console.log("首页onAppear")
      })
    }
    .mode(NavigationMode.Stack)
    .hideToolBar(true)
    .hideBackButton(true)
    .hideTitleBar(true)
    .navDestination(this.routerMap)
  }
}
HarmonyOS
22h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

参考示例如下:

import { CommonConstant } from './CommonConstant';
import { DialogUtil } from './DialogUtil';
import { LoadingDialog2 } from './LoadingDialog2';
import { ViewModel } from './ViewModel';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @Watch('showLoading')
  @State viewModel: ViewModel = ViewModel.getInstance()
  @State loadingPageStatus: string = this.viewModel.loadingPageStatus;
  loadingDialog2Controller: CustomDialogController = new CustomDialogController({
    builder: LoadingDialog2({}),
    autoCancel: false, // 允许点击蒙层关闭弹窗
    customStyle: true, // 使用自定义样式
    maskColor: '#f3f5f8',
    alignment: DialogAlignment.Center,
  })

  async aboutToAppear() {
    AppStorage.setOrCreate<CustomDialogController>('loadingDialog2Controller', this.loadingDialog2Controller);
    this.viewModel.getLiveCalendarCourse();
  }

  build() {
    Column() {
      Text('viewModel.loadingPageStatus')
        .fontSize(20)
        .visibility(this.viewModel.loadingPageStatus === CommonConstant.LOADING ? Visibility.Visible : Visibility.None)
        .backgroundColor(Color.Red)

      Text('loadingPageStatus')
        .fontSize(20)
        .visibility(this.loadingPageStatus === CommonConstant.LOADING ? Visibility.Visible : Visibility.None)
        .backgroundColor(Color.Blue)
    }
    .height('100%')
    .width('100%')
  }

  showLoading() {
    console.log('使用watch触发了showLoading方法')
    if (this.viewModel.loadingPageStatus === CommonConstant.LOADING) {
      DialogUtil.open2();
    } else {
      DialogUtil.close2();
    }
  }
}
分享
微博
QQ
微信
回复
19h前
相关问题
HarmonyOS class创建对话框不能显示
69浏览 • 1回复 待解决
HarmonyOS 弹出对话框
1浏览 • 1回复 待解决
HarmonyOS 对话框弹出页面被遮挡
77浏览 • 1回复 待解决
HarmonyOS 页面跳转后对话框不消失
24浏览 • 1回复 待解决
如何封装一个自定义Dialog对话框
2242浏览 • 1回复 待解决
如何监听Viewmodel里变量值变化
1978浏览 • 1回复 待解决