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

做深圳门户网站起什么名字好太原网站定制

做深圳门户网站起什么名字好,太原网站定制,电子商务网站开发过程,有没有专门做图的网站什么是keep-alive “keep-alive” 是 Vue.js 中的一个特殊组件#xff0c;用于缓存组件的状态#xff0c;以提高应用性能。在 Vue.js 中#xff0c;组件通常是动态创建和销毁的#xff0c;当切换到另一个页面或组件时#xff0c;之前的组件会被销毁#xff0c;再次进入时…什么是keep-alive “keep-alive” 是 Vue.js 中的一个特殊组件用于缓存组件的状态以提高应用性能。在 Vue.js 中组件通常是动态创建和销毁的当切换到另一个页面或组件时之前的组件会被销毁再次进入时会重新创建和初始化。这样可能导致组件的状态丢失需要重新初始化增加了资源的消耗。 组件解决了这个问题它可以将组件缓存起来而不是销毁使得组件在再次进入时保持之前的状态以及避免重复的创建和初始化过程。这样可以大幅度提高组件的加载速度和性能。 keep-alive的作用 keep-alive 的作用是在 Vue.js 应用中缓存组件的状态以提高应用性能和用户体验。它可以将组件暂时保存在内存中而不是每次都重新创建和初始化组件。 主要的作用有以下几点 组件状态保持通过使用 keep-alive在组件被切换时其状态会被保留。这意味着组件内部的数据、状态以及一些计算结果都会被缓存不会因为组件的销毁而丢失。当再次进入该组件时它会恢复到之前的状态而不需要重新初始化。这对于用户在不同页面或组件间切换时提供了更流畅的体验。 减少资源消耗如果没有使用 keep-alive每次切换到一个组件时都需要重新创建和初始化组件。对于复杂的组件这可能会导致不必要的资源消耗例如重新加载数据、执行复杂的计算等。而使用 keep-alive组件被缓存起来下次再次进入时直接从缓存中恢复避免了重复的初始化过程大大减少了资源消耗。 优化性能由于避免了重复的创建和初始化过程使用 keep-alive 可以显著提高组件的加载速度加快页面响应时间从而提供更好的用户体验。 需要注意的是keep-alive 并不是适用于所有组件的特别是对于一些动态变化的组件如果希望每次进入时都重新初始化或者希望释放组件占用的资源就不应该使用 keep-alive。 要使用 keep-alive只需将需要缓存的组件包裹在 keep-alive 标签中即可Vue.js 会自动管理缓存和组件的生命周期。这是一个简单但强大的功能可在合适的场景下大幅度提升应用性能。 原理 keep-alive 的原理主要涉及两个方面组件缓存和生命周期的管理。 组件缓存 当一个组件被包裹在 keep-alive 标签中时Vue.js 会将该组件的实例缓存起来而不是销毁它。组件的缓存是通过一个名为 cache 的对象来管理的该对象会保存被缓存的组件实例。当切换到一个被缓存的组件时Vue.js 首先检查 cache 对象中是否已经有该组件的缓存实例。如果有就直接从缓存中取出该实例如果没有就创建一个新的组件实例并将其缓存起来。 生命周期的管理 在切换到一个被缓存的组件时组件的生命周期钩子函数并不会被触发而是会触发 keep-alive 自己的生命周期钩子函数。keep-alive 组件有两个主要的生命周期钩子函数created 和 destroyed。在组件第一次被缓存时created 钩子函数会被触发表示 keep-alive 组件已经创建此时会创建被缓存组件的实例并将其缓存起来。在切换到其他组件时destroyed 钩子函数会被触发表示 keep-alive 组件将被销毁此时会销毁所有缓存的组件实例。 需要注意的是被包裹在 keep-alive 标签中的组件必须具有唯一的标识否则会导致缓存冲突。默认情况下Vue.js 使用组件的名称作为缓存的标识但也可以通过 key 属性来指定唯一的标识。 使用 keep-alive 时要注意以下几点 不是所有组件都适合使用 keep-alive对于一些动态变化的组件或者需要每次进入时重新初始化的组件应该避免使用 keep-alive。缓存的组件仍然会触发 activated 和 deactivated 生命周期钩子函数可以在这两个钩子函数中处理一些特定的逻辑。如果被缓存的组件包含了一些依赖于外部状态如路由参数、Vuex 状态等的逻辑需要特别注意在重新进入组件时是否需要重新更新这些状态。 总的来说keep-alive 提供了一种简单且强大的机制来优化 Vue.js 应用的性能特别是在频繁切换组件的场景下。 使用 当您使用 keep-alive 组件时通常需要将需要缓存的组件包裹在 keep-alive 标签中并为每个被缓存的组件设置一个唯一的 key 属性以确保缓存的正确性。下面是一个使用 keep-alive 组件的示例 假设我们有两个组件一个是用于显示用户信息的组件 UserProfile另一个是用于显示用户订单信息的组件 UserOrders。我们希望在用户切换到 UserProfile 组件时保持该组件的状态并且在用户切换到 UserOrders 组件后再切换回来时不重新初始化 UserProfile 组件。 templatedivkeep-alive!-- 使用 key 属性确保组件的正确缓存 --component :iscurrentComponent :keycurrentComponent //keep-alivebutton clickshowUserProfileShow User Profile/buttonbutton clickshowUserOrdersShow User Orders/button/div /templatescript import UserProfile from ./UserProfile.vue; import UserOrders from ./UserOrders.vue;export default {components: {UserProfile,UserOrders,},data() {return {currentComponent: UserProfile, // 初始显示用户信息组件};},methods: {showUserProfile() {this.currentComponent UserProfile;},showUserOrders() {this.currentComponent UserOrders;},}, }; /script在上面的示例中使用了动态组件 component :iscurrentComponent 来动态地切换显示 UserProfile 和 UserOrders 组件。同时将 keep-alive 标签包裹在动态组件外部这样 keep-alive 会缓存当前被显示的组件。 在切换组件时使用 key 属性来确保缓存的正确性。当切换到不同的组件时key 的值会变化这会触发 keep-alive 的重新缓存行为。 注意key 属性应该是唯一的以确保每个组件都能被正确地缓存。在实际应用中可能需要根据组件的具体情况设置不同的 key 值。 理解源码 keep-alive 组件的源码相对比较复杂涉及到 Vue.js 的虚拟 DOM、组件实例管理、生命周期管理等方面。下面简要介绍 keep-alive 的关键源码部分以便了解其基本原理。 在 Vue.js 的源码中keep-alive 组件是由一个特殊的内置组件 KeepAlive 实现的。它的主要作用是处理组件的缓存和管理缓存组件的生命周期。 组件的缓存实现 KeepAlive 组件内部维护了一个名为 cache 的对象用于存储缓存的组件实例。在切换到一个被缓存的组件时KeepAlive 组件首先会检查 cache 对象是否已经有该组件的缓存实例。如果缓存中有该组件实例KeepAlive 直接返回缓存的组件实例如果没有KeepAlive 会创建一个新的组件实例并将其缓存起来。 组件生命周期的管理 KeepAlive 组件有两个重要的生命周期钩子函数created 和 destroyed。在 created 钩子函数中KeepAlive 会监听父组件的 include 和 exclude 属性的变化以决定是否缓存某个组件。在切换到被缓存组件时KeepAlive 会触发 activated 生命周期钩子函数并从 cache 中取出对应的缓存组件实例。如果没有缓存实例会触发被缓存组件的 created 生命周期。在切换到其他组件时KeepAlive 会触发 deactivated 生命周期钩子函数并将当前缓存的组件实例暂时从 cache 中移除。如果需要缓存则缓存的组件实例并不会被销毁。 组件销毁时的处理 在 destroyed 钩子函数中KeepAlive 会销毁所有缓存的组件实例并清空 cache 对象。 如果想深入了解 keep-alive 的源码实现可以查阅 Vue.js 的 GitHub 仓库并浏览相关代码 keep-alive源码。 这个是从github拿的源码有兴趣可以研究一下。 import { isRegExp, isArray, remove } from shared/util import { getFirstComponentChild } from core/vdom/helpers/index import type VNode from core/vdom/vnode import type { VNodeComponentOptions } from types/vnode import type { Component } from types/component import { getComponentName } from ../vdom/create-componenttype CacheEntry {name?: stringtag?: stringcomponentInstance?: Component }type CacheEntryMap Recordstring, CacheEntry | nullfunction _getComponentName(opts?: VNodeComponentOptions): string | null {return opts (getComponentName(opts.Ctor.options as any) || opts.tag) }function matches(pattern: string | RegExp | Arraystring,name: string ): boolean {if (isArray(pattern)) {return pattern.indexOf(name) -1} else if (typeof pattern string) {return pattern.split(,).indexOf(name) -1} else if (isRegExp(pattern)) {return pattern.test(name)}/* istanbul ignore next */return false }function pruneCache(keepAliveInstance: { cache: CacheEntryMap; keys: string[]; _vnode: VNode },filter: Function ) {const { cache, keys, _vnode } keepAliveInstancefor (const key in cache) {const entry cache[key]if (entry) {const name entry.nameif (name !filter(name)) {pruneCacheEntry(cache, key, keys, _vnode)}}} }function pruneCacheEntry(cache: CacheEntryMap,key: string,keys: Arraystring,current?: VNode ) {const entry cache[key]if (entry (!current || entry.tag ! current.tag)) {// ts-expect-error can be undefinedentry.componentInstance.$destroy()}cache[key] nullremove(keys, key) }const patternTypes: ArrayFunction [String, RegExp, Array]// TODO defineComponent export default {name: keep-alive,abstract: true,props: {include: patternTypes,exclude: patternTypes,max: [String, Number]},methods: {cacheVNode() {const { cache, keys, vnodeToCache, keyToCache } thisif (vnodeToCache) {const { tag, componentInstance, componentOptions } vnodeToCachecache[keyToCache] {name: _getComponentName(componentOptions),tag,componentInstance}keys.push(keyToCache)// prune oldest entryif (this.max keys.length parseInt(this.max)) {pruneCacheEntry(cache, keys[0], keys, this._vnode)}this.vnodeToCache null}}},created() {this.cache Object.create(null)this.keys []},destroyed() {for (const key in this.cache) {pruneCacheEntry(this.cache, key, this.keys)}},mounted() {this.cacheVNode()this.$watch(include, val {pruneCache(this, name matches(val, name))})this.$watch(exclude, val {pruneCache(this, name !matches(val, name))})},updated() {this.cacheVNode()},render() {const slot this.$slots.defaultconst vnode getFirstComponentChild(slot)const componentOptions vnode vnode.componentOptionsif (componentOptions) {// check patternconst name _getComponentName(componentOptions)const { include, exclude } thisif (// not included(include (!name || !matches(include, name))) ||// excluded(exclude name matches(exclude, name))) {return vnode}const { cache, keys } thisconst key vnode.key null? // same constructor may get registered as different local components// so cid alone is not enough (#3269)componentOptions.Ctor.cid (componentOptions.tag ? ::${componentOptions.tag} : ): vnode.keyif (cache[key]) {vnode.componentInstance cache[key].componentInstance// make current key freshestremove(keys, key)keys.push(key)} else {// delay setting the cache until updatethis.vnodeToCache vnodethis.keyToCache key}// ts-expect-error can vnode.data can be undefinedvnode.data.keepAlive true}return vnode || (slot slot[0])} }
http://www.pierceye.com/news/404309/

相关文章:

  • 51单片机可以做网站怎么建设游戏试玩平台网站
  • 汕头网站建设方案维护wordpress百度熊掌
  • 牛街网站建设产品vi设计都包括什么
  • 网站需要多大宽带网站发展的方向
  • 陈光锋网站运营推广新动向故城建设银行网站
  • 备案后网站可以改名吗临颖网站建设
  • 临沭县建设局官方网站怎样做外贸网站推广
  • 手机网站支付一个简单的网页代码带图片
  • 向公司申请请做网站广州网站推广教程
  • 用QQ群做网站排名交互式网站app
  • 正规免费发布信息网站国外网站界面
  • 浏览国外网站 dns网店运营推广方案
  • wordpress弹幕视频插件广西seo搜索引擎优化
  • 网站开发与维护工资多少网络公司排名兴田德润
  • wordpress主题ux壹搜网站建设优化排名
  • 试剂产品商城网站建设杭州网站现场备案
  • 高唐企业建网站服务商wordpress google
  • 重庆网站开发商城最近新闻有哪些
  • 电商网站设计线路图有哪些网络推广平台
  • 海门市建设局网站科技与应用
  • 北京做网站s免费做app网站有哪些
  • 免费制作网页的网站网络营销师报名官网
  • 长沙网站制作好公司网络服务模型
  • 网站开发的时间流程微信平台可以做微网站吗
  • 镇江网站seo天猫网店代运营
  • 吴江城乡住房和城乡建设局网站怎么给别人做网站优化
  • 名师工作室网站建设 意义网站图片上浮动文字
  • 做co的网站商城网站不备案
  • 黄山建设网站公司电话网站下载链接怎么做
  • 开发企业网站多少钱电视剧排行榜百度搜索风云榜