Web组件中的预加载,如何实现?

WebView的预加载。

HarmonyOS
2024-05-26 17:34:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
PinkFish

使用的核心API

  • prefetchPage
  •  prepareForPageLoad

核心代码解释

prefetchPage中放置的参数为预加载的url,其作用是在在预测到将要加载的页面之前调用,提前下载页面所需的资源,包括主资源子资源,但不会执行网页JavaScript代码或呈现网页,以加快加载速度。

import UIAbility from '@ohos.app.ability.UIAbility'; 
import web_webview from '@ohos.web.webview'; 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import Want from '@ohos.app.ability.Want'; 
import router from '@ohos.router'; 
​ 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 
    console.log("EntryAbility onCreate") 
    web_webview.WebviewController.initializeWebEngine() 
    // 预连接时,需要將'https://www.example.com'替换成一个真实的网站地址。 
    web_webview.WebviewController.prepareForPageLoad("https://xw.qq.com", true, 2); 
    AppStorage.setOrCreate("abilityWant", want) 
    AlertDialog.show({message:"EntryAbility onCreate done"}) 
    console.log("EntryAbility onCreate done") 
  } 
} 
​ 
@Entry 
@Component 
struct tests{ 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
  aboutToAppear() { 
    // 配置web开启调试模式 
    web_webview.WebviewController.setWebDebuggingAccess(true); 
  } 
​ 
  build() { 
    Column() { 
      Button('prefetchPopularPage') 
        .onClick(() => { 
          try { 
            // 预加载时,需要将'https://www.example.com'替换成一个真实的网站地址。 
            this.controller.prefetchPage('https://xw.qq.com/'); 
            //AlertDialog.show({message: '进入try'}) 
          } catch (error) { 
            console.error(`ErrorCode: ${error.code},  Message: ${error.message}`); 
            AlertDialog.show({message:error.message}) 
          } 
        }) 
      // 需要将'www.example1.com'替换成一个真实的网站地址。 
      Web({ src: 'xw.qq.com', controller: this.controller}) 
      //Web({ src:$rawfile('test.html'), controller: this.controller }) 
      Button('返回') 
       .onClick(()=>{ 
         router.replaceUrl({  url:'pages/Index' }) 
       }) 
        .margin({left:270,top:-800}) 
    } 
​ 
  } 
}

新建一个list列表,点击其中一个跳转之相应的Web界面

import util from '@ohos.util'; 
import router from '@ohos.router'; 
 
class Contact { 
  key: string = util.generateRandomUUID(true); 
  name: string; 
  icon: Resource; 
 
  constructor(name: string, icon: Resource) { 
    this.name = name; 
    this.icon = icon; 
  } 
} 
 
class dividerTmp { 
  strokeWidth: Length = 1 
  startMargin: Length = 10 
  endMargin: Length = 10 
  color: ResourceColor = '#ffe9f0f0' 
 
  constructor(strokeWidth: Length, startMargin: Length, endMargin: Length, color: ResourceColor) { 
    this.strokeWidth = strokeWidth 
    this.startMargin = startMargin 
    this.endMargin = endMargin 
    this.color = color 
  } 
} 
 
let opt: dividerTmp = new dividerTmp(1, 10, 10, '#ffe9f0f0') 
 
@Entry 
@Component 
export default struct Header { 
  @State needWrap: boolean = true 
  private swiperController: SwiperController = new SwiperController(); 
  private contacts: Array<object> = [ 
    new Contact('加拿大.............................', $r("app.media.icon")), 
    new Contact('中秋节,又称团圆节,象征着团圆与美满。', $r("app.media.icon")), 
    new Contact('小红', $r("app.media.icon")), 
  ] 
  //Tab 
  private tabsController: TabsController = new TabsController() 
  @State currentIndex: number = 0; 
 
  @Builder 
  TabBuilder(title: string, targetIndex: number) { 
    Column() { 
      Text(title) 
        .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') 
    } 
    .onClick(() => { 
      this.currentIndex = targetIndex; 
      this.tabsController.changeIndex(this.currentIndex); 
    }) 
  } 
 
  build() { 
    Column({ space: 12 }) { 
      Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) { 
        TabContent() { 
          Column() { 
            Tabs() { 
              // 顶部导航栏内容 
            } 
          } 
          .backgroundColor('#ffffffff') 
          .width('100%') 
          .offset({ x: 0, y: 0 }) 
 
        } 
        .tabBar(this.TabBuilder('首页', 0)) 
 
        TabContent() { 
          Text('发现的内容').fontSize(30).backgroundColor(Color.Orange) 
        } 
        .tabBar(this.TabBuilder('发现', 1)) 
 
        TabContent() { 
          Text('推荐的内容').fontSize(30).backgroundColor(Color.Pink) 
        }.tabBar(this.TabBuilder('推荐', 2)) 
 
        TabContent() { 
        }.tabBar(this.TabBuilder('我的', 3)) 
      }.onClick(() => { 
        // this.currentIndex = index 
      }) 
      .scrollable(false) 
 
      GridRow() { 
        GridCol({ span: { sm: 12, md: 6, lg: 5 } }) { 
          Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) { 
            Search({ placeholder: '猜您喜欢: 万水千山' }) 
              .placeholderFont({ size: 16 }) 
              .margin({ top: -760, bottom: 1000 }) 
            Image($r('app.media.startIcon')) 
              .width(32) 
              .height(32) 
              .objectFit(ImageFit.Contain) 
              .flexShrink(0) 
              .margin({ top: -760, bottom: 1000 }) 
          } 
        } 
      }.onBreakpointChange((breakpoint: string) => { 
        if (breakpoint === 'sm') { 
          this.needWrap = true 
        } else { 
          this.needWrap = false 
        } 
      }) 
      .padding({ left: 12, right: 12 }) 
 
      Swiper(this.swiperController) { 
        Text("暂无内容") 
          .width('96%') 
          .height('12%') 
          .backgroundColor(Color.Orange) 
          .textAlign(TextAlign.Center) 
          .fontSize(30) 
 
        Text("暂无内容") 
          .width('96%') 
          .height('12%') 
          .backgroundColor(Color.Brown) 
          .textAlign(TextAlign.Center) 
          .fontSize(30) 
 
        Text("暂无内容") 
          .width('96%') 
          .height('12%') 
          .backgroundColor(Color.Pink) 
          .textAlign(TextAlign.Center) 
          .fontSize(30) 
      } 
      .loop(true) 
      .autoPlay(true) 
      .interval(2800) 
      .margin({ top: -1000 }) 
 
      List({ space: 10 }) { 
        ForEach(this.contacts, (item: Contact) => { 
          ListItem() { 
            Row() { 
              Text(item.name) 
                .width("82%") 
                .fontSize(20) 
                .onClick(() => { 
                  router.replaceUrl({ 
                    url: 'pages/test' 
                  }) 
                }) 
 
              Image(item.icon) 
                .width(40) 
                .height(40) 
                .margin(8) 
            } 
            .width('96%') 
            .margin({ left: 12 }) 
            .justifyContent(FlexAlign.Start) 
          } 
        }, (item: Contact) => item.key.toString()) 
      } 
      .divider(opt) 
      .width('100%') 
 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-05-27 22:16:04
相关问题
TabContent组件是否支持加载
382浏览 • 1回复 待解决
如何禁止web组件自动加载图片?
357浏览 • 1回复 待解决
web组件对html文件加载
374浏览 • 1回复 待解决
Web组件如何判断网址是否加载成功
786浏览 • 1回复 待解决
使用web组件实现预览沙箱pdf
535浏览 • 1回复 待解决
电脑端窗口关闭实现
277浏览 • 1回复 待解决
Web组件如何实现高度自适应?
407浏览 • 1回复 待解决
JS API web组件 怎么使用
3866浏览 • 1回复 待解决
富文本在web组件无法展示
465浏览 • 1回复 待解决