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

效果好的郑州网站建设职业中学网站建设

效果好的郑州网站建设,职业中学网站建设,桂林腾达网是什么网,怎么做有个捐款的网站什么是 keep-alive&#xff1f; <keep-alive> 是一个内置组件&#xff0c;用于缓存组件实例&#xff0c;从而提高应用的性能。当包裹动态组件时&#xff0c;<keep-alive> 会缓存不活跃的组件实例&#xff0c;而不是销毁它们。这使得当组件重新激活时&#xff0c;可…

什么是 keep-alive

<keep-alive> 是一个内置组件,用于缓存组件实例,从而提高应用的性能。当包裹动态组件时,<keep-alive> 会缓存不活跃的组件实例,而不是销毁它们。这使得当组件重新激活时,可以保留其状态,避免重新渲染,从而提升用户体验和性能。

主要用途

  1. 缓存组件状态:当组件在不同路由之间切换时,<keep-alive> 可以缓存组件的状态,避免每次切换时重新渲染。
  2. 优化性能:通过缓存组件实例,减少不必要的 DOM 操作和计算,提高应用的响应速度。

 基本用法:

<template><div><button @click="activeComponent = 'ComponentA'">Component A</button><button @click="activeComponent = 'ComponentB'">Component B</button><keep-alive><component :is="activeComponent"></component></keep-alive></div>
</template><script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';export default {components: {ComponentA,ComponentB},data() {return {activeComponent: 'ComponentA'};}
};
</script>

在这个例子中,<keep-alive> 包裹了 <component>,当 activeComponent 切换时,组件实例会被缓存而不是销毁。

  1. **include 和 exclude**:用于控制哪些组件需要缓存,支持字符串、正则表达式或数组。
<keep-alive include="ComponentA, ComponentB" exclude="ComponentC"><router-view></router-view>
</keep-alive>

2. **max**:用于指定缓存的组件数量,当超出这个数量时,最久未使用的组件实例将被销毁。

<keep-alive :max="10"><router-view></router-view>
</keep-alive>
与 <router-view> 一起使用:
<template><div><router-link to="/a">Component A</router-link><router-link to="/b">Component B</router-link><keep-alive :include="['ComponentA']" :max="10"><router-view></router-view></keep-alive></div>
</template>

keep-alive 的源码分析

export default {name: 'KeepAlive',abstract: true, // 这是一个抽象组件,表示它不会直接渲染到 DOM 上props: {include: patternTypes, // 要缓存的组件exclude: patternTypes, // 不缓存的组件max: [String, Number] // 最大缓存数},created () {this.cache = Object.create(null); // 缓存对象this.keys = []; // 用来记录缓存的顺序},destroyed () {for (const key in this.cache) {pruneCacheEntry(this.cache, key, this.keys);}},watch: {include (val) {pruneCache(this, name => matches(val, name));},exclude (val) {pruneCache(this, name => !matches(val, name));}},render () {const slot = this.$slots.default;const vnode = getFirstComponentChild(slot); // 获取第一个子组件if (vnode) {const componentOptions = vnode.componentOptions;const name = getComponentName(componentOptions);if (name && ((this.include && !matches(this.include, name)) ||(this.exclude && matches(this.exclude, name)))) {return vnode; // 如果不匹配 include/exclude,直接返回,不缓存}const key = vnode.key == null? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : ''): vnode.key;if (this.cache[key]) {vnode.componentInstance = this.cache[key].componentInstance; // 从缓存中取出实例remove(this.keys, key); // 移除旧的位置this.keys.push(key); // 重新放到最后,更新 LRU 位置} else {this.cache[key] = vnode; // 缓存新实例this.keys.push(key);// 如果超过最大缓存数,移除最早的实例if (this.max && this.keys.length > parseInt(this.max)) {pruneCacheEntry(this.cache, this.keys[0], this.keys, this._vnode);}}vnode.data.keepAlive = true; // 标记组件为 keep-alive}return vnode || (slot && slot[0]); // 返回 vnode}
};

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

相关文章:

  • 怎么做pc端移动网站蒙文网站建设的意义
  • 手机培训网站建设WordPress adsen
  • 开放平台 的优势 传统门户网站织梦做的网站被黑了
  • 山西响应式网站平台做网站需要提供什么
  • 给手机做网站的公司监控网站模板下载
  • 凡科快速建站wordpress用qq注册
  • 电子商务网站开发时间进度表七牛云存储代替WordPress
  • 笑话网站域名长沙网站制作哪家
  • 北京网站建设找降龙上海优化公司
  • 天津网站制作维护邢台163手机端
  • 金华竞价排名 金华企业网站建设海外网络推广专员招聘
  • 网站开发和软件开发有什么区别网站域名个人备案跟企业备案
  • aspcms 生成网站地图汽车网站网址大全
  • django 做网站的代码字体 添加 wordpress
  • 网站提供的链接广告推广软件
  • 做电商网站的公司简介恒佳企划网站建设
  • 手机零售网站 关键词初中文化如何提升学历
  • 零基础学习网站开发区块链开发工程师招聘
  • 如何做企业网站php西安网页设计师
  • 海南做网站找谁网站建设有多少公司
  • 广州番禺职业技术学院官网成都seo优化
  • 泉州网站建设轩奇网讯公众号文章怎么导入wordpress
  • 教育行业建设网站采购销售管理软件
  • 网站建设意向书没有网站怎么做百度竞价
  • 大型网站制作流程网站备案需要那些资料
  • fwa 网站 欣赏网页版qq在线登录
  • 甘肃省酒泉市做网站公司个人网店搭建思路设计
  • 辽宁平台网站建设价位做网站那个程序好
  • 广西长长路桥建设有限公司网站做网站设计师的感想
  • 培训会网站建设昆明网络推广招聘