Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the Vue routers history?

I need to detect whether or not the Vue router has any more entries in its history to go back to. I would use this to detect whether or not to trigger the exit app function. As long as the app can go back to a previous page, it should, but when it comes to the end it should exit.

like image 553
Simon Hyll Avatar asked May 01 '17 05:05

Simon Hyll


People also ask

What is history in VUE router?

HTML5 History Mode The default mode for Vue Router is hash mode. It uses a URL hash to simulate a full URL so that the page won't be reloaded when the URL changes. We can set Vue Router to history mode to get rid of the hash. It uses history. pushState API to let us navigate URLs without a page reload.

How do you delete history on Vue router?

"There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location. replace() method, which replaces the current item of the session history with the provided URL."

How do I access my Vue router state?

To access Vuex state when defining Vue Router routes, we can import the store in the file with the Vue Router hooks. import Vuex from "vuex"; const store = new Vuex. Store({ state: { globalError: "", }, mutations: { setGlobalError(state, error) { state.


1 Answers

So I had a similar issue, running both an angularjs and vue.js app side by side. Once the vue.js history stack was empty, go to angularjs. Here is my vuex solution I used:

/** 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>

I'm using clientRouter here because of SSR but you can use whichever router. Also its in typescript, I can convert it to vanilla if requested. With this method you should always go back using the action here. Then all you need is a way to record the history, I used this code on 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()
}
like image 70
Amrit Kahlon Avatar answered Oct 11 '22 09:10

Amrit Kahlon