一、经过keep-alive和router-view实现路由缓存,具体代码以下:
在app.vue:vue
<router-view v-if="!$route.meta.keepAlive"/>
<keep-alive>
<router-view v-if="$route.meta.keepAlive"/>
</keep-alive>
在路由文件中配置相关参数,来判断该路由是否须要缓存,当keepAlive存在而且值为true时,缓存该路由缓存
{
path: 'dCustomerPage',
name: '客户管理',
component: dCustomerPage,
meta:{
keepAlive: true,
}
},
二、关于activated钩子函数
首次进入该路由的页面时会触发created,mounted,activated等钩子函数,但使用this.router.go1返回该路由的时候,不会触发created和mounted,只会触发activated。若是调用this.router.go(−1)返回该路由的时候,不会触发created和mounted,只会触发activated。若是调用this.router.go(-1)返回该路由后但愿手动刷新数据,能够在activated(){}中执行相关的请求,但首次进入或者f5刷新界面的时候会屡次触发相关数据请求,能够用定时器解决或者相似锁的操做。app
三、关于deactivated钩子函数
在离开该缓存路由后,该路由的组件并无被销毁,若是须要销毁如定时器相关的操做能够在deactivated钩子函数中处理,相似于beforeDestroy钩子函数的调用函数