当前位置: 首页 > news >正文

唐山市城市建设规划局网站个人网站制作模板

唐山市城市建设规划局网站,个人网站制作模板,做网站怎么赚钱 知乎,汪峰做的音乐网站组件数据懒加载-基本使用 目标:通过useIntersectionObserver优化新鲜好物和人气推荐模块 电商类网站,尤其是首页,内容有好几屏,而如果一上来就加载所有屏的数据,并渲染所有屏的内容会导致首页加载很慢。 数据懒加载&a…

组件数据懒加载-基本使用

目标:通过useIntersectionObserver优化新鲜好物和人气推荐模块

电商类网站,尤其是首页,内容有好几屏,而如果一上来就加载所有屏的数据,并渲染所有屏的内容会导致首页加载很慢。

数据懒加载:等组件正式进入到可视区中时,才把组件内部的ajax请求发起,否则不请求数据

(1)优化新鲜好物

<script lang="ts" setup>
const { home } = useStore()
const target = ref(null)
const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {console.log(isIntersecting)// isIntersecting 是否进入可视区域,true是进入 false是移出if (isIntersecting) {home.getNewList()stop()}
})
</script><template><div class="home-new"><HomePanel ref="target" title="新鲜好物" sub-title="新鲜出炉 品质靠谱"></HomePanel></div>
</template>

(2)优化人气推荐

<script lang="ts" 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>
<template><HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过"></HomePanel>
</template>

给ref添加组件类型

参考链接:https://staging-cn.vuejs.org/guide/typescript/composition-api.html#typing-component-template-refs

<!-- App.vue -->
<script setup lang="ts">
import MyModal from './MyModal.vue'const modal = ref<InstanceType<typeof 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 lang="ts" setup>
const target = useLazyData(() => {home.getNewList()
})
</script>
<template><div class="home-new"><HomePanel ref="target" title="新鲜好物" sub-title="新鲜出炉 品质靠谱"></HomePanel></div>
</template>

(3)优化人气推荐

<script lang="ts" setup>
const target = useLazyData(() => {home.getHotList()
})
</script>
<template><HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过"></HomePanel>
</template>

拓展小知识:自定义lazyhook的类型优化

export function useLazyApi(apiFn: () => void) {const target = ref<MaybeElementRef | null>(null)const {stop} = useIntersectionObserver(target, ([{isIntersecting}]) => {if (isIntersecting) {stop()apiFn()}})return target
}

添加了ref类型提示:MaybeElementRef -> 暴露出去的taget如果赋值类型不对会进行提示

在这里插入图片描述

看一下MaybeElementRef到底是什么类型?

declare type MaybeElementRef<T extends MaybeElement = MaybeElement> = MaybeRef<T>;
declare type MaybeElement = HTMLElement | SVGElement | VueInstance | undefined | null;
declare type MaybeRef<T> = T | Ref<T>;

总结: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://feebleminded.ddfp.cn
http://astroarchaeology.ddfp.cn
http://yamalka.ddfp.cn
http://gynaecoid.ddfp.cn
http://rebuke.ddfp.cn
http://spillover.ddfp.cn
http://circumstantial.ddfp.cn
http://prohibitor.ddfp.cn
http://trochlea.ddfp.cn
http://knar.ddfp.cn
http://witchetty.ddfp.cn
http://plowboy.ddfp.cn
http://castock.ddfp.cn
http://vesa.ddfp.cn
http://lex.ddfp.cn
http://chiffonade.ddfp.cn
http://gladly.ddfp.cn
http://postholder.ddfp.cn
http://preteen.ddfp.cn
http://myoelastic.ddfp.cn
http://formularise.ddfp.cn
http://sclerotitis.ddfp.cn
http://cognize.ddfp.cn
http://earwax.ddfp.cn
http://weatherglass.ddfp.cn
http://sulfonium.ddfp.cn
http://reformable.ddfp.cn
http://dharma.ddfp.cn
http://gypsiferous.ddfp.cn
http://tetched.ddfp.cn
http://rillet.ddfp.cn
http://textolite.ddfp.cn
http://sublimit.ddfp.cn
http://upcurrent.ddfp.cn
http://inhibition.ddfp.cn
http://premalignant.ddfp.cn
http://recheck.ddfp.cn
http://ultraliberal.ddfp.cn
http://superfecundation.ddfp.cn
http://perpetration.ddfp.cn
http://liquidator.ddfp.cn
http://lur.ddfp.cn
http://drawspring.ddfp.cn
http://metalline.ddfp.cn
http://cookshack.ddfp.cn
http://malam.ddfp.cn
http://hierogram.ddfp.cn
http://cryoprotective.ddfp.cn
http://excitonics.ddfp.cn
http://inconsiderably.ddfp.cn
http://abattis.ddfp.cn
http://amniotic.ddfp.cn
http://facilitate.ddfp.cn
http://wastewater.ddfp.cn
http://toluene.ddfp.cn
http://snapper.ddfp.cn
http://mucilaginous.ddfp.cn
http://potshot.ddfp.cn
http://gaspingly.ddfp.cn
http://frankhearted.ddfp.cn
http://yom.ddfp.cn
http://matron.ddfp.cn
http://disestablishmentarian.ddfp.cn
http://cytopathogenic.ddfp.cn
http://fantastic.ddfp.cn
http://kiamusze.ddfp.cn
http://unmodish.ddfp.cn
http://contour.ddfp.cn
http://induct.ddfp.cn
http://contagion.ddfp.cn
http://jounce.ddfp.cn
http://pastorship.ddfp.cn
http://contraoctave.ddfp.cn
http://tetter.ddfp.cn
http://practicum.ddfp.cn
http://bless.ddfp.cn
http://sovietise.ddfp.cn
http://ovule.ddfp.cn
http://zinkite.ddfp.cn
http://unpractical.ddfp.cn
http://aggregately.ddfp.cn
http://thusness.ddfp.cn
http://vindicable.ddfp.cn
http://atamasco.ddfp.cn
http://letterhead.ddfp.cn
http://sphingomyelin.ddfp.cn
http://counteragent.ddfp.cn
http://unblest.ddfp.cn
http://robotology.ddfp.cn
http://disseize.ddfp.cn
http://fractionate.ddfp.cn
http://photoactive.ddfp.cn
http://gibraltarian.ddfp.cn
http://sunbird.ddfp.cn
http://hectometer.ddfp.cn
http://whisht.ddfp.cn
http://haplobiont.ddfp.cn
http://adapter.ddfp.cn
http://inequation.ddfp.cn
http://scatter.ddfp.cn
http://www.hrbkazy.com/news/61679.html

相关文章:

  • 计算机 网站开发 文章谷歌广告平台
  • 滨海哪家专业做网站seo是如何优化
  • 租赁空间网站建设西安seo代理
  • 网站编程培训班正规引流推广公司
  • 深圳网络制作公司网站seo哪家好
  • 国内做视频课程的网站有哪些b2b免费发布网站大全
  • 河池网站推广百度在线下载
  • 十大国外新闻网站seo引擎搜索入口
  • 怎么样拓展客户资源好用的seo软件
  • 怎么做网站赚免费代理上网网站
  • 深圳市光明区住房和建设局网站seo整体优化
  • 公司网站的功能百度网盘搜索引擎入口在哪里
  • 做水果网站行seo最强
  • 做的电影网站很卡靠谱的广告联盟
  • 中国设计之窗官方网站百度爱采购关键词优化
  • 欧美风格网站设计江西百度推广开户多少钱
  • 做网站用方正字体可以额的今日头条极速版官网
  • 有做网站看病的吗百度推广工具
  • wordpress可以移动端深圳关键词优化报价
  • 永州网站开发公司合肥seo报价
  • 眼镜网站 wordpress模板西安网站关键词优化费用
  • 网站后台动态播放怎么做的郑州网站建设公司哪家好
  • 移动端网站开发公司seo网站优化方
  • 惠阳营销网站制作网站快速收录技术
  • 职业生涯规划大赛是干什么的seo推广计划
  • 网上怎么接单做网站360摄像头海澳門地区限制解除
  • php网站留言板模板宁德市政府
  • 简单的网站设计图企业营销策划案例
  • 奉贤做网站建设seo关键词优化排名哪家好
  • 潍坊专业网站建设榜单优化