首先搭建好vue框架后
在src文件夹下创建一个router文件夹,router文件夹下创建router.js文件
router.js
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router) //让vue使用Router
const router = [
{
path: "/index",
component: ()=>import("../components/index.vue")
},
{
path:"/",
component: ()=>import("../components/login.vue")
},
{
path:"/login",
component: ()=>import("../components/login.vue")
}
];
var rou =new Router(
{
routes: router
})
rou.beforeEach((to,from,next)=>{
if(to.path!=="/login"){
if(localStorage.getItem("islogin")=="true"){
return next();
}else{
return next("/login")
}
}
return next();
})
export default rou;