Page.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="app-page-container">
  3. <h1 @click="click">RouterTab 实例页</h1>
  4. <p>你在<strong>{{second}}</strong>秒前打开本页面</p>
  5. <input type="text">
  6. <dl>
  7. <dt>name</dt>
  8. <dd>{{$route.name}}</dd>
  9. <dt>path</dt>
  10. <dd>{{$route.path}}</dd>
  11. <dt>params</dt>
  12. <dd>{{$route.params}}</dd>
  13. <dt>query</dt>
  14. <dd>{{$route.query}}</dd>
  15. <dt>hash</dt>
  16. <dd>{{$route.hash}}</dd>
  17. <dt>fullPath</dt>
  18. <dd>{{$route.fullPath}}</dd>
  19. </dl>
  20. </div>
  21. </template>
  22. <style lang="scss" scoped>
  23. .app-page-container {
  24. padding: 15px;
  25. font-size: 14px;
  26. line-height: 1.5;
  27. dt {
  28. float: left;
  29. width: 150px;
  30. font-weight: 700;
  31. }
  32. dd {
  33. min-height: 1.5em;
  34. }
  35. }
  36. </style>
  37. <script>
  38. export default {
  39. name: 'router-tab-page',
  40. data () {
  41. return {
  42. openTime: new Date(),
  43. second: 0,
  44. routerTab: {
  45. title: '页签实例' + this.$route.params.id
  46. }
  47. }
  48. },
  49. activated () {
  50. this.updateOpenTime()
  51. },
  52. deactivated () {
  53. this.clearOpenTimeInterval()
  54. },
  55. beforeDestroy () {
  56. this.clearOpenTimeInterval()
  57. },
  58. // 页面离开前提示
  59. beforePageLeave (resolve, reject, tab, type) {
  60. const action = (type === 'close' && '关闭') ||
  61. (type === 'refresh' && '刷新') ||
  62. (type === 'replace' && '替换')
  63. const msg = `您确认要${action}页签“${tab.title}”吗?`
  64. if (confirm(msg)) {
  65. resolve()
  66. } else {
  67. reject('拒绝了页面离开')
  68. }
  69. /* this.$confirm(msg, '提示', { closeOnHashChange: false })
  70. .then(resolve)
  71. .catch(reject) */
  72. },
  73. methods: {
  74. update () {
  75. this.second = Math.floor((new Date() - this.openTime) / 1000)
  76. },
  77. updateOpenTime () {
  78. this.update()
  79. this.clearOpenTimeInterval()
  80. // 定时更新事件
  81. this.openTimeInterval = setInterval(this.update, 1000)
  82. },
  83. clearOpenTimeInterval () {
  84. clearInterval(this.openTimeInterval)
  85. },
  86. click () {
  87. console.log('aaa')
  88. }
  89. }
  90. }
  91. </script>