|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <q-tabs inverted> |
| 4 | + <q-route-tab slot="title" to="/back/a" label="A" /> |
| 5 | + <q-route-tab slot="title" to="/back/b" label="B" /> |
| 6 | + <q-route-tab slot="title" to="/back/c" label="C" /> |
| 7 | + </q-tabs> |
| 8 | + |
| 9 | + <div> |
| 10 | + {{modals}} |
| 11 | + <q-btn @click="add" label="Add" /> |
| 12 | + <q-btn @click="remove" label="Remove" /> |
| 13 | + </div> |
| 14 | + |
| 15 | + <br> |
| 16 | + |
| 17 | + <router-view /> |
| 18 | + </div> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script> |
| 22 | +export default { |
| 23 | + data () { |
| 24 | + return { |
| 25 | + modals: 0 |
| 26 | + } |
| 27 | + }, |
| 28 | + methods: { |
| 29 | + add () { |
| 30 | + console.log('add') |
| 31 | + this.modals++ |
| 32 | + const state = window.history.state || {} |
| 33 | + state.modal = this.modals |
| 34 | + window.history.replaceState(state, '') |
| 35 | + window.history.pushState({hasBack: true}, '') |
| 36 | + if (this.modals === 1) { |
| 37 | + console.log('add popstate') |
| 38 | + window.addEventListener('popstate', this.__popstate) |
| 39 | + } |
| 40 | + }, |
| 41 | + remove (next) { |
| 42 | + console.log('remove') |
| 43 | + if (this.modals) { |
| 44 | + this.modals-- |
| 45 | + window.history.go(-1) |
| 46 | + console.log('remove: history back') |
| 47 | + if (next) { |
| 48 | + console.log('remove: calling next') |
| 49 | + next() |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + __popstate () { |
| 54 | + console.log('popstate', window.history.state) |
| 55 | + if (window.history.state && window.history.state.modal) { |
| 56 | + this.modals-- |
| 57 | + window.history.state.modal = null |
| 58 | + if (this.modals === 0) { |
| 59 | + console.log('remove popstate') |
| 60 | + window.removeEventListener('popstate', this.__popstate) |
| 61 | + } |
| 62 | + } |
| 63 | + if (this.push) { |
| 64 | + const to = this.push |
| 65 | + console.log('pushing', to) |
| 66 | + this.push = null |
| 67 | + this.$router.push(to) |
| 68 | + } |
| 69 | + else if (this.replace) { |
| 70 | + const to = this.replace |
| 71 | + console.log('replacing', to) |
| 72 | + this.replace = null |
| 73 | + this.$router.replace(to) |
| 74 | + } |
| 75 | + else { |
| 76 | + console.log('back detected; replacing state') |
| 77 | + // window.history.replaceState({}, '') |
| 78 | + } |
| 79 | + }, |
| 80 | + __clearBogusHistory () { |
| 81 | + console.log('__clearBogusHistory') |
| 82 | + window.removeEventListener('popstate', this.__clearBogusHistory) |
| 83 | + if (window.history.state && window.history.state.hasBack) { |
| 84 | + console.log('__clearBogusHistory: hasBack; going back in history') |
| 85 | + window.addEventListener('popstate', this.__clearBogusHistory) |
| 86 | + window.history.go(-1) |
| 87 | + } |
| 88 | + } |
| 89 | + }, |
| 90 | + created () { |
| 91 | + console.log('layout created') |
| 92 | + this.__clearBogusHistory() |
| 93 | + this.beforeEach = this.$router.beforeEach((to, from, next) => { |
| 94 | + console.log('route change', from, to) |
| 95 | + console.log('HISTORY length', window.history.length) |
| 96 | + if (this.modals) { |
| 97 | + if (window.history.state) { |
| 98 | + console.log('HISTORY. modal: ', window.history.state.modal, 'hasBack:', window.history.state.hasBack) |
| 99 | + } |
| 100 | + const prop = window.history.state && window.history.state.hasBack |
| 101 | + ? 'push' |
| 102 | + : 'replace' |
| 103 | + console.log('going back', prop) |
| 104 | + this[prop] = to |
| 105 | + next(false) |
| 106 | + window.history.go(-1) |
| 107 | + } |
| 108 | + else { |
| 109 | + console.log('next') |
| 110 | + next() |
| 111 | + } |
| 112 | + }) |
| 113 | + }, |
| 114 | + beforeDestroy () { |
| 115 | + console.log('layout beforeDestroy') |
| 116 | + this.beforeEach() |
| 117 | + } |
| 118 | +} |
| 119 | +</script> |
0 commit comments