vue2.0页面缓存和不缓存的方法:
1、在app中设置需要缓存的div
<keep-alive>//缓存的页面 <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"></router-view>//不缓存的页面
2、在路由router.js中设置.vue页面是否需要缓存
{ path: '/home', component: home, meta: { keepAlive: true },//当前的.vue文件需要缓存 }, { path: '/notice', component: notice,//当前页面不需要缓存 }
3、从缓存页面跳转到不缓存页面,或者从不缓存页面跳转到缓存页面的时候,会发现watch是不能监听路由的,是因为缓存和不缓存页面分别在不同的div里面,一个div里面是不可能监听到另一个div的路由的,所有需要把监听的路由都加上缓存(在路由添加 meta: { keepAlive: true }),路由在缓存页面之间进行跳转的时候,就可以通过监听路由来进行判断数据是否需要更新。
watch: { '$route' (to, from) { if( from.path == "/index"){ console.log(888) } } }