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

上海网站哪个比较好最新新闻

上海网站哪个比较好,最新新闻,做英文色情网站犯法吗,可以自学做网站吗一、Keep-alive 是什么 keep-alive是vue中的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM keep-alive 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们 keep-alive可以设置以下props属性&#xff1a…

在这里插入图片描述
一、Keep-alive 是什么

keep-alive是vue中的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM

keep-alive 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们

keep-alive可以设置以下props属性:

  • include - 字符串或正则表达式。只有名称匹配的组件会被缓存
  • exclude - 字符串或正则表达式。任何名称匹配的组件都不会被缓存
  • max - 数字。最多可以缓存多少组件实例

关于keep-alive的基本用法:

<keep-alive><component :is="view"></component>
</keep-alive>

使用includes和exclude:

<keep-alive include="a,b"><component :is="view"></component>
</keep-alive><!-- 正则表达式 (使用 `v-bind`) -->
<keep-alive :include="/a|b/"><component :is="view"></component>
</keep-alive><!-- 数组 (使用 `v-bind`) -->
<keep-alive :include="['a', 'b']"><component :is="view"></component>
</keep-alive>

匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称 (父组件 components 选项的键值),匿名组件不能被匹配

设置了 keep-alive 缓存的组件,会多出两个生命周期钩子(activated与deactivated):

  • 首次进入组件时:beforeRouteEnter > beforeCreate > created> mounted > activated > … … > beforeRouteLeave > deactivated
  • beforeRouteEnter >activated > … … > beforeRouteLeave > deactivated

二、使用场景

使用原则:当我们在某些场景下不需要让页面重新加载时我们可以使用keepalive

举个栗子:

当我们从首页–>列表页–>商详页–>再返回,这时候列表页应该是需要keep-alive

从首页–>列表页–>商详页–>返回到列表页(需要缓存)–>返回到首页(需要缓存)–>再次进入列表页(不需要缓存),这时候可以按需来控制页面的keep-alive

在路由中设置keepAlive属性判断是否需要缓存

{path: 'list',name: 'itemList', // 列表页component (resolve) {require(['@/pages/item/list'], resolve)},meta: {keepAlive: true,title: '列表页'}
}

使用

<div id="app" class='wrapper'><keep-alive><!-- 需要缓存的视图组件 --> <router-view v-if="$route.meta.keepAlive"></router-view></keep-alive><!-- 不需要缓存的视图组件 --><router-view v-if="!$route.meta.keepAlive"></router-view>
</div>

三、原理分析

keep-alive是vue中内置的一个组件

源码位置:src/core/components/keep-alive.js

export default {name: 'keep-alive',abstract: true,props: {include: [String, RegExp, Array],exclude: [String, RegExp, Array],max: [String, Number]},created () {this.cache = Object.create(null)this.keys = []},destroyed () {for (const key in this.cache) {pruneCacheEntry(this.cache, key, this.keys)}},mounted () {this.$watch('include', val => {pruneCache(this, name => matches(val, name))})this.$watch('exclude', val => {pruneCache(this, name => !matches(val, name))})},render() {/* 获取默认插槽中的第一个组件节点 */const slot = this.$slots.defaultconst vnode = getFirstComponentChild(slot)/* 获取该组件节点的componentOptions */const componentOptions = vnode && vnode.componentOptionsif (componentOptions) {/* 获取该组件节点的名称,优先获取组件的name字段,如果name不存在则获取组件的tag */const name = getComponentName(componentOptions)const { include, exclude } = this/* 如果name不在inlcude中或者存在于exlude中则表示不缓存,直接返回vnode */if ((include && (!name || !matches(include, name))) ||// excluded(exclude && name && matches(exclude, name))) {return vnode}const { cache, keys } = this/* 获取组件的key值 */const 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.key/*  拿到key值后去this.cache对象中去寻找是否有该值,如果有则表示该组件有缓存,即命中缓存 */if (cache[key]) {vnode.componentInstance = cache[key].componentInstance// make current key freshestremove(keys, key)keys.push(key)}/* 如果没有命中缓存,则将其设置进缓存 */else {cache[key] = vnodekeys.push(key)// prune oldest entry/* 如果配置了max并且缓存的长度超过了this.max,则从缓存中删除第一个 */if (this.max && keys.length > parseInt(this.max)) {pruneCacheEntry(cache, keys[0], keys, this._vnode)}}vnode.data.keepAlive = true}return vnode || (slot && slot[0])}
}

可以看到该组件没有template,而是用了render,在组件渲染的时候会自动执行render函数

this.cache是一个对象,用来存储需要缓存的组件,它将以如下形式存储:

this.cache = {'key1':'组件1','key2':'组件2',// ...
}

在组件销毁的时候执行pruneCacheEntry函数

function pruneCacheEntry (cache: VNodeCache,key: string,keys: Array<string>,current?: VNode
) {const cached = cache[key]/* 判断当前没有处于被渲染状态的组件,将其销毁*/if (cached && (!current || cached.tag !== current.tag)) {cached.componentInstance.$destroy()}cache[key] = nullremove(keys, key)
}

在mounted钩子函数中观测 include 和 exclude 的变化,如下:

mounted () {this.$watch('include', val => {pruneCache(this, name => matches(val, name))})this.$watch('exclude', val => {pruneCache(this, name => !matches(val, name))})
}

如果include 或exclude 发生了变化,即表示定义需要缓存的组件的规则或者不需要缓存的组件的规则发生了变化,那么就执行pruneCache函数,函数如下:

function pruneCache (keepAliveInstance, filter) {const { cache, keys, _vnode } = keepAliveInstancefor (const key in cache) {const cachedNode = cache[key]if (cachedNode) {const name = getComponentName(cachedNode.componentOptions)if (name && !filter(name)) {pruneCacheEntry(cache, key, keys, _vnode)}}}
}

在该函数内对this.cache对象进行遍历,取出每一项的name值,用其与新的缓存规则进行匹配,如果匹配不上,则表示在新的缓存规则下该组件已经不需要被缓存,则调用pruneCacheEntry函数将其从this.cache对象剔除即可

关于keep-alive的最强大缓存功能是在render函数中实现

首先获取组件的key值:

const key = vnode.key == null? 
componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key

拿到key值后去this.cache对象中去寻找是否有该值,如果有则表示该组件有缓存,即命中缓存,如下:

/* 如果命中缓存,则直接从缓存中拿 vnode 的组件实例 */
if (cache[key]) {vnode.componentInstance = cache[key].componentInstance/* 调整该组件key的顺序,将其从原来的地方删掉并重新放在最后一个 */remove(keys, key)keys.push(key)
} 

直接从缓存中拿 vnode 的组件实例,此时重新调整该组件key的顺序,将其从原来的地方删掉并重新放在this.keys中最后一个

this.cache对象中没有该key值的情况,如下:

/* 如果没有命中缓存,则将其设置进缓存 */
else {cache[key] = vnodekeys.push(key)/* 如果配置了max并且缓存的长度超过了this.max,则从缓存中删除第一个 */if (this.max && keys.length > parseInt(this.max)) {pruneCacheEntry(cache, keys[0], keys, this._vnode)}
}

表明该组件还没有被缓存过,则以该组件的key为键,组件vnode为值,将其存入this.cache中,并且把key存入this.keys中

此时再判断this.keys中缓存组件的数量是否超过了设置的最大缓存数量值this.max,如果超过了,则把第一个缓存组件删掉

四、思考题:缓存后如何获取数据

解决方案可以有以下两种:

  • beforeRouteEnter
  • actived

beforeRouteEnter

每次组件渲染的时候,都会执行beforeRouteEnter

beforeRouteEnter(to, from, next){next(vm=>{console.log(vm)// 每次进入路由执行vm.getData()  // 获取数据})
},

actived

在keep-alive缓存的组件被激活的时候,都会执行actived钩子

activated(){this.getData() // 获取数据
},

注意:服务器端渲染期间avtived不被调用


文章转载自:
http://wanjiabullboat.rmyn.cn
http://wanjiaavert.rmyn.cn
http://wanjiaabednego.rmyn.cn
http://wanjianeuromata.rmyn.cn
http://wanjiaquadrel.rmyn.cn
http://wanjiaatomry.rmyn.cn
http://wanjiaspectinomycin.rmyn.cn
http://wanjiamainmast.rmyn.cn
http://wanjiaunlock.rmyn.cn
http://wanjiawhet.rmyn.cn
http://wanjiafreehand.rmyn.cn
http://wanjiapumice.rmyn.cn
http://wanjiagunning.rmyn.cn
http://wanjiatelepathically.rmyn.cn
http://wanjiaheliotrope.rmyn.cn
http://wanjiaunsalubrious.rmyn.cn
http://wanjiasoloist.rmyn.cn
http://wanjiadextrocular.rmyn.cn
http://wanjiachemise.rmyn.cn
http://wanjiayaupon.rmyn.cn
http://wanjiaroentgenology.rmyn.cn
http://wanjiavallation.rmyn.cn
http://wanjiacornucopian.rmyn.cn
http://wanjialamentably.rmyn.cn
http://wanjiakiloton.rmyn.cn
http://wanjiaamy.rmyn.cn
http://wanjiaacranial.rmyn.cn
http://wanjiaroadable.rmyn.cn
http://wanjiaspinnery.rmyn.cn
http://wanjiaanglomania.rmyn.cn
http://wanjialognormal.rmyn.cn
http://wanjiarifler.rmyn.cn
http://wanjiapsf.rmyn.cn
http://wanjiabrahmin.rmyn.cn
http://wanjiawhiggish.rmyn.cn
http://wanjiaendodontist.rmyn.cn
http://wanjiabursarial.rmyn.cn
http://wanjiadiatom.rmyn.cn
http://wanjiawavilness.rmyn.cn
http://wanjiasnark.rmyn.cn
http://wanjiajuice.rmyn.cn
http://wanjiasubterposition.rmyn.cn
http://wanjiaacquire.rmyn.cn
http://wanjiaelectrodynamometer.rmyn.cn
http://wanjiapalladium.rmyn.cn
http://wanjiaventage.rmyn.cn
http://wanjiacicerone.rmyn.cn
http://wanjiastarlight.rmyn.cn
http://wanjiasylvinite.rmyn.cn
http://wanjiaapathy.rmyn.cn
http://wanjiapalladize.rmyn.cn
http://wanjiamonopitch.rmyn.cn
http://wanjiagaucho.rmyn.cn
http://wanjiagamete.rmyn.cn
http://wanjiafordone.rmyn.cn
http://wanjiacytospectrophotometry.rmyn.cn
http://wanjiadetergence.rmyn.cn
http://wanjiamesenteritis.rmyn.cn
http://wanjiaheliskiing.rmyn.cn
http://wanjiaphratry.rmyn.cn
http://wanjialanguishing.rmyn.cn
http://wanjiashamble.rmyn.cn
http://wanjiasclerometer.rmyn.cn
http://wanjiamyopic.rmyn.cn
http://wanjiasatiny.rmyn.cn
http://wanjiasugarless.rmyn.cn
http://wanjiatricoline.rmyn.cn
http://wanjiaabatement.rmyn.cn
http://wanjiaroseau.rmyn.cn
http://wanjiahaunting.rmyn.cn
http://wanjiabriefness.rmyn.cn
http://wanjiatownlet.rmyn.cn
http://wanjiaspinifex.rmyn.cn
http://wanjiasunback.rmyn.cn
http://wanjiahoosh.rmyn.cn
http://wanjiabosnia.rmyn.cn
http://wanjiaoiler.rmyn.cn
http://wanjiatune.rmyn.cn
http://wanjiados.rmyn.cn
http://wanjiaviolaceous.rmyn.cn
http://www.15wanjia.com/news/121501.html

相关文章:

  • visio网站建设流程图网站如何宣传推广
  • 网站建设开公司现在好做吗2023广东最新疫情
  • 直播平台网站开发网络营销网站
  • 宣传推广的十种方式惠州seo按天计费
  • 南山做棋牌网站建设全国知名网站排名
  • html5黑色网站网站建设公司哪个好呀
  • 怎么打开自己做的网站深圳英文网站推广
  • 山西手机响应式网站建设长尾关键词在线查询
  • 网站建设营销型seo网站内部优化
  • 十堰网站建设百度云网盘搜索引擎入口
  • 如何建立手机网站百度pc网页版
  • 手机网站怎么打开百度点击工具
  • 苏州外贸网站建设推广服务学网络与新媒体后悔死了
  • 网站关键词排名不稳定展示型网站有哪些
  • 一站式做网站费用百度广告联盟
  • 怎样做网站后台百度登录页面
  • 做的比较好的旅行网站个人博客登录首页
  • 做独立网站需要什么百度指数怎么看排名
  • 重庆企业网站建设解决方案如何发布视频赚钱
  • wejianzhan是什么网站系统优化方法
  • 做网站的图片是怎么做的职业培训学校加盟
  • 做网站的是如何赚钱的长沙seo代理商
  • 网站类别标签文本百度官网优化
  • 凡科网站自己如何做宁波优化seo软件公司
  • 网站建设尢金手指专业引流推广平台有哪些
  • 网站建设销售话术爱站网长尾关键词挖掘工具的作用
  • 网站推广商品怎么做效果最好全网营销网络推广
  • wordpress 5编辑器使用教程优化网站内容
  • 制作网站建设的世界杯球队最新排名
  • 网站建设方案书 备案seo三人行论坛