HarmonyOS webInited这个事件有时候触发有时候不触发

import { webview } from '@kit.ArkWeb';
import {WebpageStatus,OffsetEnum, SpanEnum,ColumnEnum} from "../constants/CommonEnums"
import {CommonConstants} from "../constants/CommonConstants"
import Logger from "../utils/Logger"
import { FailureLayout } from './FailureLayout';
import { LoadingLayout } from './LoadingLayout';
import { TopNavigationView } from './TopNavigationView';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG = '[WebView]';
// @Entry({ routeName: 'WebView' })
@Component
export struct WebView {
  onDestinationBack: () => boolean = () => false;
  handlePageEnd?: () => void;
  shouldShowExtBtn?: () => void;
  hiddenScrollBar?: boolean = false;
  url: string = '';
  name:string="";
  showHeader:boolean=true;
  isLarge?: boolean = true;
  @Link webViewController: webview.WebviewController;
  @State webStatus: WebpageStatus = WebpageStatus.INIT;
  @State ua: string = "";
  aboutToAppear() {
    this.webViewController = new webview.WebviewController();
    webview.WebviewController.setWebDebuggingAccess(true);
    webview.once('webInited', () => {
      try {
        // 应用侧用法示例,定制UserAgent。
        this.ua = this.webViewController.getUserAgent();
        this.webViewController.setCustomUserAgent(this.ua);
        Logger.info(TAG, 'webInited userAgent: ' + this.webViewController.getCustomUserAgent());
      } catch (error) {
        console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
      }
    })

  }
  build() {
    NavDestination() {
      GridRow({
        columns: this.isLarge ? { sm: ColumnEnum.SM, md: ColumnEnum.MD, lg: ColumnEnum.LG } : 1,
        gutter: {
          x: {
            sm: $r('app.float.xxl_padding_margin'),
            md: $r('app.float.md_padding_margin'),
            lg: $r('app.float.md_padding_margin')
          }
        },
        breakpoints: { reference: BreakpointsReference.ComponentSize }
      }) {
        GridCol({
          span: this.isLarge ? { sm: SpanEnum.SM, md: SpanEnum.MD, lg: SpanEnum.LG } : 1,
          offset: this.isLarge ? { sm: OffsetEnum.SM, md: OffsetEnum.MD, lg: OffsetEnum.LG } : 0
        }) {
          Column() {
            if(this.showHeader){
              TopNavigationView({ title: this.name, onDestinationBack: () => this.onDestinationBack() })
            }

            Stack() {

              Web({ controller: this.webViewController, src: this.url })
                .darkMode(WebDarkMode.Auto)
                .domStorageAccess(true)
                .zoomAccess(true)
                .fileAccess(true)
                .mixedMode(MixedMode.All)
                .cacheMode(CacheMode.None)
                .verticalScrollBarAccess(!this.hiddenScrollBar)
                .javaScriptAccess(true)
                .width(CommonConstants.FULL_PERCENT)
                .onProgressChange((event) => {
                  Logger.info(TAG, 'newProgress:' + event?.newProgress);
                  if (event?.newProgress === 100) {
                    this.shouldShowExtBtn && this.shouldShowExtBtn();
                  }
                  Logger.info(TAG, 'onProgressChange weburl: ' + this.webViewController.getUrl());
                })
                .onPageBegin(() => {
                  Logger.info(TAG, ' onPageBegin start loading');
                  Logger.info(TAG, 'onPageBegin weburl: ' + this.webViewController.getUrl());
                  Logger.info(TAG, 'onPageBegin userAgent: ' + this.webViewController.getCustomUserAgent());
                })
                .onErrorReceive(() => {
                  Logger.info(TAG, 'onErrorReceive: error');
                  this.webStatus = WebpageStatus.FINISHED;
                })
                .onPageEnd((event) => {
                  Logger.info(TAG, 'onPageEnd loading completed url: ' + event?.url);
                  Logger.info(TAG, 'onPageEnd weburl: ' + this.webViewController.getUrl());
                  if (this.webStatus != WebpageStatus.ERROR) {
                    this.webStatus = WebpageStatus.FINISHED;
                    this.handlePageEnd && this.handlePageEnd();
                  }
                })

              if (this.webStatus === WebpageStatus.INIT) {
                Column() {
                  LoadingLayout()
                }
                .backgroundColor(Color.White)
              }

              if (this.webStatus === WebpageStatus.ERROR) {
                Column() {
                  FailureLayout({
                    handleReload: () => {
                      this.webViewController.refresh();
                      this.webStatus = WebpageStatus.INIT;
                    }
                  })
                }
                .justifyContent(FlexAlign.Center)
                .alignItems(HorizontalAlign.Center)
                .backgroundColor(Color.White)
                .width(CommonConstants.FULL_PERCENT)
                .height(CommonConstants.FULL_PERCENT)
              }
            }
          }
        }
      }
      .layoutWeight(1)
    }
    .width("100%")
    .hideTitleBar(true)
    .onShown(() => {
    })
    .onHidden(() => {
    })
    .onBackPressed(() => this.onDestinationBack())
  }
}

根据日志来看,我多次运行程序,webInited这个事件为什么有时候触发有时候不触发,为啥?

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

Navigation组件的页面跳转行为不会导致Web页面重新初始化,而webInited事件是Web组件的一个事件,用于在Web初始化完成时触发回调。其触发条件是当Web组件完成初始化过程后,会触发该事件。所以使用navigation跳转只有第一次打开这个webview页面的时候才会触发webInited的callback事件是正常的

分享
微博
QQ
微信
回复
1天前
相关问题
有时候报错,有时候又报,汗……
278浏览 • 1回复 待解决
有时候我需要更新或替换一些库。
199浏览 • 1回复 待解决
HarmonyOS 事件触发相关
62浏览 • 1回复 待解决
HarmonyOS navigation触发onpageshow
9浏览 • 1回复 待解决
HarmonyOS onKeyEvent 按钮触发
29浏览 • 1回复 待解决
HarmonyOS onPageShow事件无法触发
34浏览 • 1回复 待解决
HarmonyOS onPageShow事件无法触发
28浏览 • 1回复 待解决
HarmonyOS 生命周期触发
80浏览 • 1回复 待解决