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

秦皇岛手机网站制作多少钱网站特色页面建设

秦皇岛手机网站制作多少钱,网站特色页面建设,泉州百度网站快速优化,整站优化推广keep-alive 1、目的 在使用组件时,有时我们需要将组件进行缓存,而不是重新渲染,用以提高性能,避免重复加载DOM,提升用户的体验; keep-alive 组件可以做到这一点,它允许你缓存组件实例&#xf…

keep-alive

1、目的

在使用组件时,有时我们需要将组件进行缓存,而不是重新渲染,用以提高性能,避免重复加载DOM,提升用户的体验
keep-alive 组件可以做到这一点,它允许你缓存组件实例,而不是销毁它们,并在需要的时候重新使用它们。

2、作用

keep-alive 组件允许你缓存组件实例,而不是销毁它们,并在需要的时候重新使用它们。这可以提高性能,避免重复加载DOM,提升用户的体验。
比如:

@params currentView :当前活跃的组件名称
<keep-alive><!-- 动态组件 --><component :is="currentView"></component>
</keep-alive>

3、用法

keep-alive 在vue3中接收三个参数
include:一个字符串或正则表达式数组,包含才会缓存该组件用于匹配 需要 缓存的组件名称或组件实例

exclude:一个字符串或正则表达式数组,不包含当前组件才会缓存;即用于匹配 不需要 缓存的组件名称或组件实例
max:一个数字,用于限制缓存的组件实例的数量。当达到这个数值时,会自动将最不活跃的组件销毁,以保证最大组件实例数量,即执行类似数组的push操作,进行先进先出的缓存策略
比如在app.vue 文件中

<keep-alive><router-view></router-view>
</keep-alive><template>
<!-- 不缓存 Header,Footer 组件--><keep-alive exclude="Header,Footer" :max="3"><component :is="currentView"></component></keep-alive><!-- 缓存 Header,Footer 组件--><keep-alive include="Header,Footer"><component :is="currentView"></component></keep-alive><!-- 使用正则表达式,缓存 Header,Footer 组件 --><keep-alive include="/Header|Footer/"><component :is="currentView"></component></keep-alive><!-- 使用数组,缓存 Header,Footer 组件 --><keep-alive include="[Header, Footer]"><component :is="currentView"></component></keep-alive>
</template>

注意
由于vue组合式api 中没有显性的进行组件名称命名(name),故使用 <script setup> 的单文件组件会自动根据文件名生成对应的 name 选项,无需再手动声明
而在 选项式api中需要显性命名
如:

<script>export default{name: 'Header' // 组件名称}
</script>

4、执行生命周期

keep-alive 组件的生命周期
1、组件第一次进入,即没有被缓存的时候,会执行 setup()、 onBeforeMount() 、onMounted()、onActivated() (没有keep-alive时候,首次执行:setup()、 onBeforeMount() 、onMounted())
2、组件被缓存的时候,会执行onActivated()
3、组件被激活的时候,会执行onActivated()
4、组件被移除的时候,会执行onDeactivated()
下图:
请添加图片描述

5、执行原理

……function pruneCache(filter?: (name: string) => boolean) {cache.forEach((vnode, key) => {const name = getComponentName(vnode.type as ConcreteComponent)if (name && (!filter || !filter(name))) {// 当前缓存的组件名称 和 当前活跃的组件名称不相同,则移除该组件pruneCacheEntry(key)}})}function pruneCacheEntry(key: CacheKey) {const cached = cache.get(key) as VNodeif (!current || !isSameVNodeType(cached, current)) {// 卸载unmount(cached)} else if (current) {// current active instance should no longer be kept-alive.// we can't unmount it now but it might be later, so reset its flag now.// 重新设置缓存标识resetShapeFlag(current)}cache.delete(key)keys.delete(key)}// prune cache on include/exclude prop change// 观测 include | exclude 的值变化,并且能观测到更新后的DOM,进行缓存更新watch(() => [props.include, props.exclude],([include, exclude]) => {include && pruneCache(name => matches(include, name))exclude && pruneCache(name => !matches(exclude, name))},// prune post-render after `current` has been updated{ flush: 'post', deep: true },)……// 判断是否达到最大缓存数量if (max && keys.size > parseInt(max as string, 10)) {pruneCacheEntry(keys.values().next().value)}
……

vue3 中,keep-alive 使用 cache 一个 Map 对象,key 是组件名称,value 是组件实例,通过缓存组件实例,避免重复加载DOM,提升用户体验。
而在vue2 中,keep-alive 使用 cache 一个 Object 对象;相比较Map 对象性能更好;

仅代表个人观点,如有错误,欢迎批评指正

http://www.yayakq.cn/news/744550/

相关文章:

  • 怎么做电商网站推广郑州网站建设精英
  • 网站开发 视频播放器大英哪里有做网站的
  • 通州重庆网站建设qq空间网页版
  • vue 做自适应网站全国建设信息网站
  • 泊头哪有哪家做网站做的号网站域名 设置
  • 网站tkd怎么做创建电子商务网站的步骤
  • 长沙住建信息中心网站做网站的代码
  • 域名备案和网站备案找个网站懂的网站
  • 手机网站模板 html5 下载工具网站怎么做404页面的跳转
  • 贵阳做网站的大公司有哪些如何做一个购物网站页面
  • dede网站图标中国建设银行人才招聘网站
  • 做问卷的网站清远专业网站建设
  • 关于我们 网站织梦网站程序下载
  • 静态网页模板源码网站seo基础优化
  • 2014网站建设国内优秀网站设计师
  • 最传统的网站推广手段大连免费建站模板
  • 怎么用lls做网站wordpress怎么导入html
  • 怎么看一个网站是不是仿站工程认证网站的建设
  • wordpress 网站投票唐山网站主页制作
  • 做兼职去哪个网站青岛网络推广公司哪家好
  • 做菠菜网站代理南京制作网页学校
  • 成都网站建设销售网站色调
  • 在网站建设工作会上的讲话wordpress 添加自定义小工具栏
  • win系统做网站臭事百科wordpress
  • 网站建设好找工作网店美工设计模板
  • 宁波外贸seo网站建设公司邮箱登陆
  • 网站的规划方案公司网站后台导航链接怎么做
  • 云南网网站网站建设拾金手指下拉
  • 贵阳网站建设zu97网站建设管理职责
  • 济南网站制作经验百度搜索使用方法