import { Component, Vue, Watch } from 'vue-property-decorator' @Component({ name: 'KeepAliveMixins', }) // eslint-disable-next-line import/no-unused-modules export default class KeepAliveMixins extends Vue { /** * 当前缓存的刷新token */ private refreshKeepAlive = '' /** * 刷新成功后的回调函数 */ private refreshCallback: any = null /** * 是否校验刷新token */ private keepaliveTokenEnable = true /** * 当前路由 */ private keepaliveCurPath = '' constructor() { super() } public created() { const { path = '', query: { refreshKeepAlive = '' }, } = this.$route this.keepaliveCurPath = path this.refreshKeepAlive = String(refreshKeepAlive) } /** * 注册刷新成功的回调函数 * @param callback 回调函数 * @param keepaliveTokenEnable 是否开启刷新token校验 */ public registerRefreshCallback(callback: () => any, keepaliveTokenEnable = true): void { this.keepaliveTokenEnable = keepaliveTokenEnable if (typeof callback !== 'undefined' && typeof callback === 'function') { this.refreshCallback = callback } } @Watch('$route') private watchRoute(route) { if (typeof route !== 'undefined' && route) { const { path = '', query: { refreshKeepAlive = '' }, } = route const validToken: boolean = this.keepaliveTokenEnable ? refreshKeepAlive !== this.refreshKeepAlive : true if (this.keepaliveCurPath === path && validToken && this.refreshCallback) { this.refreshCallback() this.refreshKeepAlive = refreshKeepAlive } } } }