【每日学点HarmonyOS Next知识】hvigor升级、Dialog动画、LocalStorage无效、页面与子组件的生命周期、cookie设置

112 阅读2分钟

1、HarmonyOS 编译工具hvigor如何升级到"hvigorVersion": "4.2.0"版本?

可以手动更新到指定版本,参考链接如下:developer.huawei.com/consumer/cn…

2、HarmonyOS 自定义Dialog如何修改弹出动画?

let anmDuration: number = 300;

// 弹窗交互
@CustomDialog
struct CustomDialogExample {
  controller: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({}),
    autoCancel: false
  })
  @State showFlag: Visibility = Visibility.Visible;
  @State isAutoCancel: boolean = false;
  textController: TextAreaController = new TextAreaController()

  build() {
    Column() {
      Row() {
        Text("自定义动画的弹窗")
      }
      .borderRadius(20)
      .backgroundColor('#ff6288cb')
      .height(200)
      .width('100%')
    }
    .margin({top:10})
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height("100%")
    .onClick(() => {
      console.log("dialogClick")
      if (this.isAutoCancel) {
        console.log("dialogClick2")
        this.cancel();
      }
    })
    .visibility(this.showFlag)
    // 定义进场出场转场动画效果
    .transition(TransitionEffect.OPACITY.animation({ duration: anmDuration })
      .combine(TransitionEffect.translate({ y: 500 })))
  }
  // 延迟关闭弹窗,让自定义的出场动画显示
  cancel() {
    this.showFlag = Visibility.Hidden
    console.log("closeDialog")
    setTimeout(() => {
      this.controller.close()
    }, anmDuration)
  }
}

@Entry
@Component
struct CustomDialogUser {
  @State isAutoCancel: boolean = true;
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({ isAutoCancel: this.isAutoCancel }),
    autoCancel: this.isAutoCancel,
    customStyle: true,
  })

  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          this.dialogController.open()
        })
    }.width('100%')
    .height('100%')
  }
}

3、HarmonyOS NavDestination中使用LocalStorage无效的问题?

项目中目前使用Navigation来做跳转。目前的问题是,用navigation打开的页面使用LocalStorage无效。

如果 用localStorage的话,@state修饰的关键词值的变化会导致页面跟着变化:

let para: Record<string, number> = { 'PropA': 47 };
let storage: LocalStorage = new LocalStorage(para);
@Entry(storage)
@Component
export default struct APage {
  @Consume('navigation') navigation: NavPathStack;
  @LocalStorageLink('PropA') storageLink: number =0;
  @State testid:number =0
  number: number = 0

  build() {
    NavDestination() {
      Column() {
        Text(JSON.stringify(this.testid))
          .fontSize("20vp")
          .textAlign(TextAlign.Center)
          .width('100%')
          .height("20vp")
          .onClick(() => {
            this.navigation.pushPath({
              name: "BPage"
            }, false);
          })

        Text("点击改变testId")
          .fontSize("20vp")
          .textAlign(TextAlign.Center)
          .width('100%')
          .height("20vp")
          .margin({top:'20vp'})
          .onClick(() => {
            this.number++
            storage.set('PropA', this.number)
            this.testid =  storage.get('PropA') as number
          })
      }
      .width("100%")
      .height('100%')
    }
  }
}

4、HarmonyOS Entry页面与子组件的生命周期执行顺序是怎么样的?

Entry的生命周期是aboutToAppear - build - onPageShow。那build中的子组件的aboutToAppear是在 build - component aboutToAppear - onPageShow,还是在build - onPageShow - component aboutToAppear。

参考页面和自定义组件生命周期文档:developer.huawei.com/consumer/cn…

应用冷启动的初始化流程为:MyComponent aboutToAppear --> MyComponent build --> MyComponent onDidBuild–> Child aboutToAppear --> Child build --> Child onDidBuild --> Index onPageShow。

image.png

5、HarmonyOS 如何在h5页面中取到WebCookieManager中设置的cookies?

参考官方文档上webviewmanager的各项操作:developer.huawei.com/consumer/cn… 使用getcookie等获取cookies的值。