HarmonyOS WebviewController 加载的网页不显示?

controller: web_webview.WebviewController = new web_webview.WebviewController()
  onPageShow() {
    const params:Params = router.getParams() as Params; // 获取传递过来的参数对象
    if (params) {
      let resultJson: Params = new Params();
      resultJson.item=params.item;
      this.AdData=resultJson.item
      this.url=this.AdData.ad_url
      console.info('http opageshow:' + this.url);
      //日志 http opageshow: https://www.huawei.com/
    }
  }
  build() {
    Column() {
      this.Title()
      Web({src:this.url, controller: this.controller })
// Web({ src: 'https://www.huawei.com', controller: this.controller })
// 这样可以显示。是哪里不对?
    }
    .width(CommonConstants.FULL_WIDTH)
    .height(CommonConstants.FULL_HEIGHT)
    .backgroundColor($r('app.color.page_background'))
  }
阅读 574
1 个回答

从代码里看到渲染web是在build方法内,而获取url是在onPageShow方法内,由于build先于onPageShow执行,因此加载web时并没有获取到url,应该在 aboutToAppear方法内获取url,这样即可正确加载

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进