1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| function useHistoryStateNavigation(base) { const { history, location } = window let currentLocation = { value: createCurrentLocation(base, location), } let historyState = { value: history.state } if (!historyState.value) { changeLocation(currentLocation.value, { back: null, current: currentLocation.value, forward: null, position: history.length - 1, replaced: true, scroll: null, }, true) } function changeLocation(to, state, replace) { const url = createBaseLocation() + (base.indexOf('#') > -1 && location.search ? location.pathname + location.search + '#' : base) + to try { history[replace ? 'replaceState' : 'pushState'](state, '', url) historyState.value = state } catch (err) { warn('Error with push/replace State', err) location[replace ? 'replace' : 'assign'](url) } } function replace(to, data) { const state = assign({}, history.state, buildState(historyState.value.back, to, historyState.value.forward, true), data, { position: historyState.value.position }) changeLocation(to, state, true) currentLocation.value = to } function push(to, data) { const currentState = assign({}, historyState.value, history.state, { forward: to, scroll: computeScrollPosition(), }) if ( !history.state) { warn(`history.state seems to have been manually replaced without preserving the necessary values. Make sure to preserve existing history state if you are manually calling history.replaceState:\n\n` + `history.replaceState(history.state, '', url)\n\n` + `You can find more information at https://next.router.vuejs.org/guide/migration/#usage-of-history-state.`) } changeLocation(currentState.current, currentState, true) const state = assign({}, buildState(currentLocation.value, to, null), { position: currentState.position + 1 }, data) changeLocation(to, state, false) currentLocation.value = to } return { location: currentLocation, state: historyState, push, replace } }
|