App.vue 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <template>
  2. <div id="app">
  3. <router-view />
  4. </div>
  5. </template>
  6. <script>
  7. import { removeToken } from '@/utils/auth'
  8. export default {
  9. name: 'App',
  10. data() {
  11. return {
  12. beforeUnloadTime: '',
  13. gapTime: ''
  14. }
  15. },
  16. mounted() {
  17. window.addEventListener('beforeunload', e => {
  18. const { path } = this.$route
  19. if (!path.includes('/perview')) {
  20. this.beforeUnloadTime = new Date().getTime()
  21. }
  22. })
  23. window.addEventListener('unload', async() => {
  24. const { path } = this.$route
  25. if (!path.includes('/perview')) {
  26. this.gapTime = new Date().getTime() - this.beforeUnloadTime
  27. if (this.gapTime <= 5) {
  28. // 关闭
  29. // 清除 cookie 或者 localStorage 的方法
  30. removeToken()
  31. } else {
  32. // 刷新
  33. }
  34. }
  35. })
  36. },
  37. destroyed() {
  38. window.removeEventListener('beforeunload')
  39. window.removeEventListener('unload')
  40. }
  41. }
  42. </script>