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