原因:未匹配到路由
router.beforeEach((to, from, next) => {
if (to.matched.length ===0) { //如果未匹配到路由
from.path? next({ path:from.path}) : next('/'); //如果上级也未匹配到路由则跳转主页面,如果上级能匹配到则转上级路由
} else {
next(); //如果匹配到正确跳转
}
});
到 router/index.js下查找是否有对应路由配置
import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap} from '@/config/router.config'
// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
export default new Router({
mode: 'history',
routes: constantRouterMap
})