建设银行网银网站特色,邮箱域名,如何上wordpress,帮境外赌场做网站是否有风险组件数据懒加载-基本使用 目标#xff1a;通过useIntersectionObserver优化新鲜好物和人气推荐模块 电商类网站#xff0c;尤其是首页#xff0c;内容有好几屏#xff0c;而如果一上来就加载所有屏的数据#xff0c;并渲染所有屏的内容会导致首页加载很慢。
数据懒加载通过useIntersectionObserver优化新鲜好物和人气推荐模块 电商类网站尤其是首页内容有好几屏而如果一上来就加载所有屏的数据并渲染所有屏的内容会导致首页加载很慢。
数据懒加载等组件正式进入到可视区中时才把组件内部的ajax请求发起否则不请求数据
1优化新鲜好物
script langts setup
const { home } useStore()
const target ref(null)
const { stop } useIntersectionObserver(target, ([{ isIntersecting }]) {console.log(isIntersecting)// isIntersecting 是否进入可视区域true是进入 false是移出if (isIntersecting) {home.getNewList()stop()}
})
/scripttemplatediv classhome-newHomePanel reftarget title新鲜好物 sub-title新鲜出炉 品质靠谱/HomePanel/div
/template2优化人气推荐
script langts setup
const { home } useStore()
const target ref(null)
const { stop } useIntersectionObserver(target, ([{ isIntersecting }]) {console.log(isIntersecting)// isIntersecting 是否进入可视区域true是进入 false是移出if (isIntersecting) {home.getHotList()stop()}
})
/script
templateHomePanel reftarget title人气推荐 sub-title人气爆款 不容错过/HomePanel
/template给ref添加组件类型
参考链接:https://staging-cn.vuejs.org/guide/typescript/composition-api.html#typing-component-template-refs
!-- App.vue --
script setup langts
import MyModal from ./MyModal.vueconst modal refInstanceTypetypeof MyModal | null(null)const openModal () {modal.value?.open()
}
/script组件数据懒加载-封装 目标封装组件数据懒加载可复用的逻辑 分析 首页中很多地方都应该使用组件数据懒加载这个功能不管是哪个模块使用下面代码都会重复书写 事实上唯一可能会随着业务使用发生变化的是 ajax接口的调用 其余的部分我们进行重复使用抽离为可复用逻辑 核心代码
1封装通用的懒加载数据api src/utils/hooks.ts
// 自定义一些通用的compositions api
import { useIntersectionObserver } from vueuse/core
import { ref } from vue// 封装通用的数据懒加载api
export function useLazyData(apiFn: () void) {// 通过 ref 获得组件实例const target ref(null)const { stop } useIntersectionObserver(// target 是观察的目标dom容器必须是dom容器而且是vue3.0方式绑定的dom对象target,// isIntersecting 是否进入可视区域true是进入 false是移出// observerElement 被观察的dom([{ isIntersecting }]) {// 在此处可根据isIntersecting来判断然后做业务if (isIntersecting) {stop()apiFn()}})return target
}2优化新鲜好物
script langts setup
const target useLazyData(() {home.getNewList()
})
/script
templatediv classhome-newHomePanel reftarget title新鲜好物 sub-title新鲜出炉 品质靠谱/HomePanel/div
/template3优化人气推荐
script langts setup
const target useLazyData(() {home.getHotList()
})
/script
templateHomePanel reftarget title人气推荐 sub-title人气爆款 不容错过/HomePanel
/template拓展小知识自定义lazyhook的类型优化
export function useLazyApi(apiFn: () void) {const target refMaybeElementRef | null(null)const {stop} useIntersectionObserver(target, ([{isIntersecting}]) {if (isIntersecting) {stop()apiFn()}})return target
}添加了ref类型提示MaybeElementRef - 暴露出去的taget如果赋值类型不对会进行提示 看一下MaybeElementRef到底是什么类型
declare type MaybeElementRefT extends MaybeElement MaybeElement MaybeRefT;
declare type MaybeElement HTMLElement | SVGElement | VueInstance | undefined | null;
declare type MaybeRefT T | RefT;总结MaybeElementRef类型的类型为
MaybeElement的Ref类型或者直接为MayBeElement类型
首页主体-滚动加载商品的bug
产品区域需要滚动比较多才能去加载数据。threshold 容器和可视区交叉的占比进入的面积/容器完整面积 取值0-1 之间默认比0大所以需要滚动较多才能触发进入可视区域事件。 阈值 (进入的面积/容器完整面积)
const { stop } useIntersectionObserver(target,([{ isIntersecting }], observerElement) {if (isIntersecting) {stop()// 调用API获取数据apiFn().then(data {result.value data.result})}},{threshold: 0}
)
rElement) {if (isIntersecting) {stop()// 调用API获取数据apiFn().then(data {result.value data.result})}},{threshold: 0}
)
文章转载自: http://www.morning.lqzhj.cn.gov.cn.lqzhj.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.jgttx.cn.gov.cn.jgttx.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.lskyz.cn.gov.cn.lskyz.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.svtxeu.com.gov.cn.svtxeu.com http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.qcdhg.cn.gov.cn.qcdhg.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.yrgb.cn.gov.cn.yrgb.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn