总结:keep-alive 是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM。结合vue-router中使用,可以缓存某个view的整个内容。

1.在App.vue中添加配置:

<!-- 缓存所有的页面 -->
<keep-alive>
<router-view v-if="$route.meta.keep_alive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keep_alive"></router-view>
 
2.在需要缓存的页面,配置路由
  {
path: '/',
name: 'headers',
component: headers,
meta:{
keep_alive:true
}
},

3.使用,子组件header.vue

<template>
<div>
<h5>header</h5>
<button @click="handclick"></button>
<input type="text" v-model="msg">
</div>
</template>
<script>
export default {
name:"header",
data(){
return{
msg:"",
}
},
methods:{
handclick(){
let that=this;
this.msg="里斯",
setTimeout(function(){
that.$router.push('/details')
},); }
}
}
</script> <style>
button{
height: 50px;
width: 50px;
background-color: red;
} </style>

4.父组件 detail.vue

<template>
<div>
<h5>我是detail页面</h5>
<keep-alive>
<head></head>
</keep-alive>
<button @click="goheader">跳转会header界面</button>
</div>
</template>
<script>
import header from "./header.vue";
export default {
components: {
header
},
data() {
return {};
},
methods: {
goheader() {
this.$router.push({ name: "headers" });
}
}
};
</script> <style>
</style>

5.include/exclude两个属性 可以针对性缓存相应的组件

include - 字符串或正则表达式。只有名称匹配的组件会被缓存。

exclude - 字符串或正则表达式。任何名称匹配的组件都不会被缓存。

<keep-alive include="a,b">
<component :is="view"></component>
</keep-alive>
<keep-alive :include="includedComponents">
<router-view></router-view>
</keep-alive>

最新文章

  1. PHP扩展安装mcrypt 提示没有可用包(No package php-mcrypt available)
  2. css3控制标题字数超出的部分自动显示为“...”省略号
  3. CentOS下安装Mysql数据库
  4. 20145308刘昊阳 《Java程序设计》实验二 Java面向对象程序设计 实验报告
  5. JavaScript 面向对象与原型
  6. 记录一次会话CRT
  7. How to make project not set to be build
  8. 重要业务MySQL冷备解决方案
  9. 前端工具之WebPack解密之背景
  10. iOS基础 - 完善键盘处理
  11. Java AOP - Aspectj
  12. spring boot整合log4j2
  13. HDFS的命令
  14. oracle报错ORA-01653 dba_free_space中没有该表空间
  15. 洛谷P3474 KUP-Plot purchase
  16. Spyder简述
  17. vsftp 虚拟用户+MySQL认证独立家目录
  18. Kubernetes (1.6) 中的存储类及其动态供给
  19. JavaScript中filter()方法
  20. ActiveMQ (一) 介绍与安装

热门文章

  1. Nginx-介绍nginx的两篇博客
  2. mem 0908
  3. 在Ubuntu 14.04.1中安装VMware Tools的步骤
  4. VBA 根据Find方法根据特定内容查找单元格
  5. JavaScript----特效代码
  6. A股行情记录
  7. Redis环境配置和命令语句
  8. webstom 快捷键
  9. layer开启与关闭加载层
  10. End-to-end and Hop-by-hop Headers ---nginx-websocket