关于填坑vue的前进刷新与后退不刷新,网上有不少方法,基本都是利用 keep-alive,可是试了好多种方法都不尽人意,后来有人提示说基于keepalive include,试验了一下找到了些思路,暂时实验可行,分享下共同探讨学习,也许不是最佳解决方案,但确实有效。这里须要用到vuex,如不用vuex能够自行用Local Storage代替。vue
<keep-alive :include="includeds"> <router-view></router-view> </keep-alive>
computed:{ includeds(){ return this.$store.state.includeds } }
state: { includes: '' }, mutations: { setIncludeds(state,setdata){ state.includeds = setdata } }
router.beforeEach((to, from, next) => { let addPage = ['addProduct','newEdit','showNews']; if (!mCd.inArray(to.name,addPage)) { //此处mCd.inArray的方法为判断数组是否包含,须要本身实现。 store.commit('setIncludeds',''); } next(); })
export default{ name: 'news', data() { return { .... } }, activated(){ this.$store.commit('setIncludeds','news'); //此处设置name一致的名称 } }
*注意:此处activated里设置的commit里第二个参数,必须与name名称一致。
1. 经过设置keepalive 的 include,固然也能够设置exclude,自行百度。include为要缓存的页面name 2. 在页面activated的时候设置为缓存当前页面。 3. 页面跳转的时候判断路由的to.name是否包含在已设置的数组中。 4. 跳转到edit或show页面,返回后回到缓存页面,不刷新。由其余页面进入则刷新。 5. 若是不设置路由全局守卫,也能够每一个页面单独写beforeRouteLeave