VueRouter的beforeEach导致错误:无法将异常转换为字符串

时间:2018-08-24 21:21:02

标签: vue.js vue-router

我的控制台说:

[vue-router] uncaught error during route navigation:
<failed to convert exception to string>

导致该错误的行是main.js中的那一行:

next('/login')

我的main.js文件:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import App from './App.vue'
import Routes from './routes'

import { store } from './store/store';


Vue.use(VueRouter);

const router = new VueRouter({
    routes: Routes
})

router.beforeEach((to, from, next) => {
    next('/login');
})

var vm = new Vue({
  el: '#app',
  store: store,
  router: router,
  render: h => h(App),
})

我的route.js文件:

export default [
    { path: '/', component: Game, cors: true },
    { path: '/login', component: Login },
    { path: '/signin', component: SignIn },
    { path: '/gamerouter', component: GameRouter },
    { path: '/waitingforplayers', component: WaitingForPlayers, name: "buba" },
    { path: '/selectPlayersForTeams', component: SelectPlayersForTeams },
    { path: '/startStatistics', component: StartStatistics },
    { path: '/gamelocked', component: GameLocked },
    { path: '/answering', component: Answering },

]

如果我输入next('/'),我也会收到此错误,但是如果我编写next()或next(false),则不会收到此错误; 有什么想法可以帮助我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

next必须不带参数来确认导航,否则您将继续触发beforeEach钩子:

router.beforeEach(function(to, from, next) {
  console.log(to.path)
  if (to.path !== '/timer') {
    next("/timer");
  } else {
    next();
  }
});