Jump between vue router non-components

the project function to be implemented

I need to make a judgment before I log in to the gateway
I encapsulate an axios that requests the backend network every time and returns a status code
when the return status is: login timeout, I router jump instead of window.location="/login"

related codes

import Vue from "vue"
import router from "vue-router"
import axios from "axios";

Vue.prototype.$ajax = axios
//axios
export default function(url,data={}){
    ......
    if(){
        ...router...//
        /*router.push("")*/
    }
}
Apr.12,2021

here router is a Router dependency that is not import Router from 'vue-router' .
it is actually a router object exported after routing for unified management.

// router.js
import Router from "vue-router";
import Home from 'Home.vue'
Vue.use(Router);

// router
export default new Router({
    routes: [
        {
          path: "/",
          name: "home",
          component: Home,
          children: [
            {
              path: "/a",
              name: "a",
              component: A
            }
        ]
})

on the page you need, introduce such an object instance, and then use the router.push method

// js
// router.js
import router from 'router.js'
...
...
func(){
...
    router.push('/');
...    
}


whether the encapsulated file is in a separate js file, you may need to import router to use router.push

import {router} from '@/router/index'

router/index.js

import Vue from 'vue'
import iView from 'iview'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import { routers } from './router'

Vue.use(VueRouter)

const RouterConfig = {
  mode: 'history',
  routes: routers
}

export const router = new VueRouter(RouterConfig)

what you need is the object after new VueRouter.


const router = new Router({
  mode: "hash",
  routes
});

window.vueRouter = router;

Mount the route to window, and then use window.vueRouter.push where needed

Menu