Encountering a similar issue, I had to manage both an angularjs and vue.js app running simultaneously. The approach I took was to switch to the angularjs app once the vue.js history stack was empty. Below is the solution I implemented using vuex:
/** store/history.ts */
import { clientRouter } from '@/views'
import { Route } from 'vue-router'
import { ActionTree, GetterTree, Module, MutationTree } from 'vuex'
import { IRootState } from '.'
export interface IHistoryState {
history: Route[]
}
const historyState: IHistoryState = {
history: []
}
const getters: GetterTree<IHistoryState, IRootState> = {
history: state => state.history
}
const mutations: MutationTree<IHistoryState> = {
pop: state => state.history.pop(),
push: (state, route: Route) => state.history.push(route)
}
const actions: ActionTree<IHistoryState, IRootState> = {
back: context => {
const history = context.state.history
if (history.length === 1) {
window.location.href = '/angular.html'
}
else {
clientRouter.back()
}
},
record: (context, route: Route) => {
const history = context.state.history
// Reloading same page
if (history.length && history[history.length - 1].name === route.name) {
context.commit('pop')
context.commit('push', route)
return false
}
// Going back
else if (history.length > 1 && history[history.length - 2].name === route.name) {
context.commit('pop')
return false
}
else {
context.commit('push', route)
return true
}
}
}
export default {
state: historyState,
getters,
mutations,
actions,
namespaced: true
} as Module<IHistoryState, IRootState>
Utilizing clientRouter due to SSR, but any router can be used. The code is in typescript, but can be converted to vanilla JS upon request. By following this method, navigation should always happen through the designated action here. To keep track of history, I added the following code to the main app component:
/** app.vue */
@Action('history/record') recordHistory: (to: Route) => forward:boolean
@Watch('$route')
function (to: Route) {
// Push to history
this.recordHistory(to)
// Analytics
analytics.trackPageView()
}