vue中的 mode:history 与路由的router.beforeEach钩子能共存吗?

匿名 (未验证) 提交于 2019-12-03 00:22:01

今天遇到一个问题,在路由定义的js中使用mode:history

import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login'
import index from '@/views/index'
Vue.use(Router)

export default new Router({
mode:'history',
routes: [
{
path: '/login',
name: 'login',
component: login
},
{
path: '/index',
name: 'index',
component: index
}
]
})


然后再main.js中使用路由的生命钩子进行登录控制

router.beforeEach((to, from, next) => {
if (to.path != '/login') {
next({ path: '/login' })
} else {
next()
}
})
但是在页面使用时,我访问index对应的页面却没有跳转login。如果去掉mode:history 重启项目后 就可以使用了 这是不是因为i这两者之间冲突 还是我写的不对呀  有没有大佬帮看下。
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!