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

最新文章

  1. Android中AlarmManager使用示例(持续更新,已经更改)
  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
  3. objective-C 自定义对象归档的实现
  4. sql数据库优化技巧汇总
  5. Try,Catch,Finally三块中如果有Return是怎么个运行顺序
  6. 图解SSL/TLS协议(转)
  7. oracle读写文件--利用utl_file包对磁盘文件的读写操作
  8. springMVC---简介
  9. .NET Core+Selenium+Github+Travis CI =&gt; SiteHistory
  10. LeetCode 上最难的链表算法题,没有之一!
  11. APP test
  12. Scrapy框架爬虫
  13. Jenkins安装以及邮件配置
  14. 给iOS开发新手送点福利,简述UIScrollView的属性和用法
  15. Redis 学习资料目录(Important)
  16. HTTP请求中的form data,request payload,query string parameters以及在node服务器中如何接收这些参数
  17. TCP 的那些事儿-1
  18. python对表格的使用
  19. 华为上机测试题(水仙花数升级版-java)
  20. memcached使用总结

热门文章

  1. Unity——射线检测(鼠标点击开关门效果)
  2. SystemParametersInfo函数说明
  3. oracle日常命令
  4. 嵌入式-C语言基础:快速选择排序实现从大到小排序
  5. 链表实现-回文palindrome判断
  6. 优雅的重启uwsgi 告别uwsgi reload过程中造成的无法请求、请求延迟等问题
  7. ctfshow web入门部分题目 (更新中)
  8. c# Winfrom桌面软件自动升级系统
  9. 7 STL-deque
  10. 1.3 Apache Hadoop的重要组成-hadoop-最全最完整的保姆级的java大数据学习资料