vue基础之keep-alvie保持历史页面数据不变,切换页面后数据不变keep-alvie
2024-10-20 16:31:26
1:路由配置文件设置
{
path: '/MenuM',
component: Layout,
redirect: '/MenuM',
children: [
{
path: 'MenuM',
name: 'MenuM',
component: () => import('@/views/MenuM'),
meta: { title: '主界面', icon: 'table', keepAlive: true }
}
]
},
2.标注需要缓存的组件,用包裹
这一步需要注意了,有的是在APP.VUE改,有的要看所在组件里改哦~~,比如上面的component: Layout,
那我们需要修改的文件就不是app.vue了,需要修改对应文件才行,
要不然就会无法缓存页面数据,出现问题
src\layout\components\AppMain.vue
<template>
<section class="app-main">
<div class="app-main-top">
<transition name="fade-transform" mode="out-in">
<div>
<keep-alive>
<router-view v-if="$route.meta.keepAlive" :key="key"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" :key="key"></router-view>
<!-- <router-view :key="key" /> -->
</div>
</transition>
</div>
</section>
</template>
<script>
export default {
name: 'AppMain',
computed: {
key() {
return this.$route.path
}
}
}
</script>
<style lang="less" scoped>
.app-main {
/*50 = navbar */
min-height: calc(100vh - 50px);
width: 100%;
position: relative;
overflow: hidden;
// background-image: url(~@/assets/image/bg01.jpg);
.app-main-top {
padding-top: 5px;
padding-bottom: 5px;
// background: rgba(255, 255, 255, 0.9);
background: rgba(255, 255, 255, 1); // 修改背景
min-height: calc(100vh - 50px);
}
}
.fixed-header + .app-main {
padding-top: 50px;
}
</style>
<style lang="scss">
// fix css style bug in open el-dialog
.el-popup-parent--hidden {
.fixed-header {
padding-right: 15px;
}
}
</style>
在router中设置需要缓存的组件
包裹需要缓存组件
页面第一次进入,钩子的触发顺序created-> mounted-> activated,退出时触发deactivated。当再次进入(前进或者后退)时,只触发activated
可以动态控制是否缓存组件,代码如下:
beforeRouteLeave(to, from, next) {
// 设置下一个路由的 meta
to.meta.keepAlive = false; // 不缓存,即刷新
next();
}
参考来源
https://www.jianshu.com/p/eeda81293826
https://zhuanlan.zhihu.com/p/48628352
最新文章
- Android中AlarmManager使用示例(持续更新,已经更改)
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
- objective-C 自定义对象归档的实现
- sql数据库优化技巧汇总
- Try,Catch,Finally三块中如果有Return是怎么个运行顺序
- 图解SSL/TLS协议(转)
- oracle读写文件--利用utl_file包对磁盘文件的读写操作
- springMVC---简介
- .NET Core+Selenium+Github+Travis CI =>; SiteHistory
- LeetCode 上最难的链表算法题,没有之一!
- APP test
- Scrapy框架爬虫
- Jenkins安装以及邮件配置
- 给iOS开发新手送点福利,简述UIScrollView的属性和用法
- Redis 学习资料目录(Important)
- HTTP请求中的form data,request payload,query string parameters以及在node服务器中如何接收这些参数
- TCP 的那些事儿-1
- python对表格的使用
- 华为上机测试题(水仙花数升级版-java)
- memcached使用总结