当前位置:网站首页>Vue组件切换时缓存组件状态即动态组件keep-alive 动态组件与异步组件
Vue组件切换时缓存组件状态即动态组件keep-alive 动态组件与异步组件
2022-07-22 23:02:00 【况几】
动态组件keep-alive
1. keep-alive缓存组件状态
<!-- keep-alive 包裹的组件将会被缓存!即B组件中更改内容,切换回A组件页面,再切换回B组件,是之前缓存的状态,而不是生成了新的B组件实例-->
<keep-alive>
<component :is="currentTabComponent"></component>
</keep-alive>
data(){
return{
currentComponent:AComponent
}
},
methods:{
huanHandle(){
this.currentComponent = this.currentComponent === AComponent ? BComponent : AComponent
}
},
components: {
AComponent,
BComponent
}
2. 参数
include: 只缓存某个组件exclude:除某个组件外其他组件缓存max:最多可以缓存多少组件实例。一旦这个数字达到了,在新实例被创建之前,已缓存组件中最久没有被访问的实例会被销毁掉。
<!-- 只缓存B组件 -->
<keep-alive include="BComponent">
<component :is="currentTabComponent"></component>
</keep-alive>
<!-- 不缓存B组件,缓存其他组件 -->
<keep-alive exclude="BComponent">
<component :is="currentTabComponent"></component>
</keep-alive>
<!-- 最多缓存10个组件,有新的组件时,最早的组件状态会被销毁掉 -->
<keep-alive :max="10">
<component :is="currentTabComponent"></component>
</keep-alive>
3. 异步组件
加载组件时,所有的组件从打开网页那一刻都加载的话网页会出现卡顿,所以一些组件可以采取异步加载的形式,需要的时候再由系统内部自行进行加载。
// 一打开网页就加载
import BComponent from './components/BComponent.vue'
// 异步加载
const BComponent = () => import('./components/BComponent.vue')
4. 加载异步组件同时可以有更多配置
const AsyncComponent = () => ({
// 需要加载的组件 (应该是一个 `Promise` 对象)
component: import('./MyComponent.vue'),
// 异步组件加载时使用的组件 --- 比如等待时,可以先加载一个等待页面
loading: LoadingComponent,
// 加载失败时使用的组件 --- 比如失败时,可以先加载一个错误页面
error: ErrorComponent,
// 展示加载时组件的延时时间。默认值是 200 (毫秒)--- 代表权重,先加载1000,再加载2000的组件
delay: 200,
// 如果提供了超时时间且组件加载也超时了,
// 则使用加载失败时使用的组件。默认值是:`Infinity`
timeout: 3000
})
边栏推荐
- Redis 持久化操作 (RDB, AOF)
- Jedis操作Redis
- Send benefits again! Calendar applet source code
- How to realize synchronized
- odbc excel--2022-07-21
- 重链剖分例题配套题解的题解
- 我们来浅谈代码语言的魅力
- PIP update a package
- Flink implements a window accumulation function through processfunction and timer Ontimer
- Layout drawing of semi customized digital inverter
猜你喜欢

又发福利!日历小程序源码

Program environment and pretreatment

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

小红书携手HMS Core,畅玩高清视界,种草美好生活

Redistemplate pipeline use

Come on, slide to the next little sister

C language function (1)

半定制数字反相器版图绘制

Redis 持久化操作 (RDB, AOF)

Qgraicsview implementation palette
随机推荐
半定制数字反相器版图绘制
Dark horse programmer - interface testing - four-day learning interface testing - the second day - Interface use case design, test points, function testing, security testing, performance testing, sing
什么时候使用UserCF,什么时候使用ItemCF?
Text align: center centered
BufferedInputStream缓冲区填充问题
Three cache strategies: cache side strategy, read/write through strategy, and write back strategy
Flynk uses liststate to implement keyedstate
appendToFile追加失败
What if Alibaba cloud international forgets its member name or login password?
Understand the interrupt system in STM32 in simple terms -- from principle to simple engineering examples -- nanny level tutorial
算法---使用最小花费爬楼梯(Kotlin)
三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略
TensorRT的插件实战(1)
Networkx visualizes graphs
容器监控三剑客CAdvisor收集监控数据 + InfluxDB储存数据 + Granfana展示图表数据的简介
Swift - 标红的修饰词
C语言中的字符串
Has the live broadcast function of the multi merchant system been used? 666 for used friends!
物联网安装调试员丨让“智慧”生活早日来临
synchronized是如何实现的