vue中keep-alive,include的指定页面缓存问题
做vue项目时,有时要在某些页面做缓存,而其它页面不要。比如:A:首页,B:获取所有订单页面,C:订单详情页面;从A(首页)进入 B(获取所有订单)时应该不缓存,B(所有订单)进入 C(订单详情)订单后时再返回B,此时B(所有订单页面)缓存。不需要再次刷新,即:A->B->C时都是刷新,而C->B->A时B缓存。在vue官方文档2.x以上有include 和 exclude 属性允许组件有条件地缓存。在这里主要用include结合vuex来实现,include 是根据组件的name值来判断的,所以三个页面组件都有自己的name才会生效(重要是B必须有name),这里name就叫A,B,C。
首先安装vuex
npm install --save vuex
安装后新建store.js
import Vue from "vue";
import Vuex from "vuex"; Vue.use(Vuex); export default new Vuex.Store({
state: {
keepAlive: []
},
mutations: {
setKeepAlive: (state, keepAlive) => {
state.keepAlive = keepAlive;
}
},
getters: {
keepAlive: state => state.keepAlive
}
});
在main.js里面引入store.js;
import store from './store'
new Vue({
el: '#app',
// 注入 store
store,
router,
components: { App },
template: '<App/>',
})
在APP.vue页面加入keepalive动态判断
<template>
<div class="app">
<keep-alive :include="keepAlive" >
<router-view/>
</keep-alive>
</div>
</template> <script type='text/javascript'>
export default {
data () {
return {}
},
computed: {
keepAlive () {
return this.$store.getters.keepAlive
}
}
}
</script>
在A(首页)进入 B时
<script>
export default {
name: 'A',
methods: {
goB() {
this.$store.commit('setKeepAlive', ['B']) //设置缓存
this.$router.push('/B')
}
}
}
</script>
在B页面设置是否缓存
<script>
export default {
name: 'B',//组件名称
beforeRouteLeave (to, from, next) {
if (to.path.indexOf('C') > -) {
this.$store.commit('setKeepAlive', ['B'])
} else {
this.$store.commit('setKeepAlive', [])
}
next()
}
}
</script>
这样就可以了。
vue中keep-alive,include的指定页面缓存问题的更多相关文章
- Vue 之keep-alive的使用,实现页面缓存
什么是keep-alive 有时候我们不希望组件被重新渲染影响使用体验: 或者处于性能考虑,避免多次重复渲染降低性能.而是希望组件可以缓存下来,维持当前的状态.这时候就需要用到keep-alive组件 ...
- 在vue中结合render函数渲染指定的组件到容器中
1.demo 项目结构: index.html <!DOCTYPE html> <html> <head> <title>标题</title> ...
- vue中使用router全局守卫实现页面拦截
一.背景 在vue项目中使用vue-router做页面跳转时,路由的方式有两种,一种是静态路由,另一种是动态路由.而要实现对路由的控制需要使用vuex和router全局守卫进行判断拦截(安全问题文章最 ...
- Vue中数组元素被替换,页面没有动态展示
原始代码 页面没有相应goodsList替换,打印goodsList数据已经被替换: (借用https://www.cnblogs.com/belongs-to-qinghua/p/11112613. ...
- Vue中如何书写js来渲染页面填充数据的部分代码
new Vue({ el:"#app" , data:{ user:{ id:"", username:"", password:" ...
- vue中改变数组或对象,页面没做出对应的渲染
原文链接 数组更新检测 变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sor ...
- vue中获取滚动table的可视页面宽度,调整表头与列对齐(每列宽度不都相同)
mounted() { // 在mounted中监听表格scroll事件 this.$refs.scrollTable.addEventListener( 'scroll',(event) => ...
- vue 中使用driver.js来进行页面分步引导
Driver.js 推荐15款最佳的 jQuery 分步引导插件 11 个超棒的 jQuery 分步指引插件
- vue keep-alive 取消某个页面缓存问题
keep-alive keep-alive是Vue提供的一个抽象组件,用来对组件进行缓存,从而节省性能,由于是一个抽象组件,所以在v页面渲染完毕后不会被渲染成一个DOM元素 <keep-aliv ...
随机推荐
- CMU Advanced DB System - MVCC
https://zhuanlan.zhihu.com/p/40208895 Mysql的MVCC实现 https://severalnines.com/database-blog/comparing- ...
- Spring使用多个 <context:property-placeholder/>
Spring中报"Could not resolve placeholder"的解决方案(引入多个properties文件) 解决方案: (1) 在Spring 3.0中,可以写: ...
- Hadoop的三种调度器FIFO、Capacity Scheduler、Fair Scheduler(转载)
目前Hadoop有三种比较流行的资源调度器:FIFO .Capacity Scheduler.Fair Scheduler.目前Hadoop2.7默认使用的是Capacity Scheduler容量调 ...
- c++ Array运用实例
myprint.hpp #include <iostream> #include <string> template <typename T> inline voi ...
- sublime text 文件列表如何忽略特定格式的文件名
1.只需要Preferences (中文首选项)里面找到setting-default(设置默认) 2.在设置面板里面找到 "folder_exclude_patterns" ...
- cmd大全
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) 1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr ...
- 小程序报错:对应的服务器 TLS 为 TLS 1.0 ,小程序要求的 TLS 版本必须大于等于 1.2
我这里出现此错误的原因是,搭载域名网站的服务器是windows2008 r2,配置的域名证书是TLS1.0版本,需要在服务器注册表中加入TLS的其他版本. 处理办法如下 小程序报错 TLS 版本必须大 ...
- excel 去掉 空单元格
Excel 2003 选中这一列,定位(CTRL+G)--定位条件--空值--确定--右键--删除. 1. 然后进行全选已输入的内容,可以使用鼠标拖动已输入的内容,也可以使用快捷键全选内容,按住ctr ...
- css3 rotateY 会盖住下面的元素
css3 rotateY 会盖住下面的元素 要适当的调整 -webkit-transform: rotateY(-40deg); -webkit-transform: rotateY(40deg);
- 2019最新版Java程序员零基础入门视频教程资料(全套)
为了解决Java学习初学者在网上找视频难的事情,本人整理了一份2019年度最新版的Java学习视频教程.希望看到这份视频的你们都能找到一份称心的工作,技术上都能得到进一步的提升,好东西就要分享给你们, ...