cache.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Vue from 'vue'
  2. import store from './store';
  3. Vue.mixin({
  4. beforeRouteEnter: function (to, from, next) {
  5. next(() => {
  6. let avueView = document.getElementById('avue-view');
  7. if (avueView && to.meta.savedPosition) {
  8. avueView.scrollTop = to.meta.savedPosition
  9. }
  10. })
  11. },
  12. beforeRouteLeave: function (to, from, next) {
  13. if (store.getters.tagList.length >= 16 && !store.getters.tagList.find(res => to.fullPath == res.value)) {
  14. this.$confirm('打开页面数超出最大限制,请先关闭其它页面!', '温馨提示', {
  15. confirmButtonText: '确定',
  16. showCancelButton: false,
  17. type: 'warning'
  18. })
  19. return
  20. }
  21. let avueView = document.getElementById('avue-view');
  22. if (from && from.meta.keepAlive) {
  23. if (avueView) {
  24. from.meta.savedPosition = avueView.scrollTop
  25. }
  26. const result = this.$route.meta.keepAlive === true && store.state.tags.tagList.some(ele => {
  27. return ele.value === this.$route.fullPath;
  28. });
  29. if (this.$vnode && !result) {
  30. from.meta.savedPosition = 0
  31. if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
  32. if (this.$vnode.componentOptions) {
  33. let key = this.$vnode.key == null ?
  34. this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '') :
  35. this.$vnode.key;
  36. let cache = this.$vnode.parent.componentInstance.cache;
  37. let keys = this.$vnode.parent.componentInstance.keys;
  38. if (cache[key]) {
  39. if (keys.length) {
  40. let index = keys.indexOf(key);
  41. if (index > -1) {
  42. keys.splice(index, 1);
  43. }
  44. }
  45. delete cache[key];
  46. }
  47. }
  48. }
  49. }
  50. }
  51. next();
  52. },
  53. });