今天遇到一个问题,在路由定义的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()
}
})