本人是使用的cookie登录 , 需要没有登录就跳转登录页
安装cookie
npm install vue-cookies --save
在main.js 全局引用
import VueCookies from 'vue-cookies'
Vue.use(VueCookies)
使用cookie , 过期时间为s
this.$cookies.get(keyName)
this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]])
this.$cookies.remove(keyName [, path [, domain]])
下面是router的index.js
router.beforeEach((to ,from ,next) => {
if (to.path === '/login'){
next()
}
const token = Vue.$cookies.get('token');
console.log(token);
if (!token || token === '' || token == null){
next('/login')
}
next()
})