当你想写发表一篇文章时候,需要登陆账号,那计算器是如果判断你是否登陆呢?后端发送一个token值,利用导航守卫就可以完成检测。代码如下,注:此处token值非后端,仅用于判断真假
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'Home',
component: Home,
meta:{
title:"首页"
}
},
{
path: '/login',
name: 'login',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
},
{
path:"/user",
name:"user",
component:()=>import("../views/User.vue")
}
]
const router = new VueRouter({
routes
})
var token="2"
router.beforeEach((to,from,next)=>{
)
if(to.path=="/home"){
// 如果跳转的路由地址是/home
if(token){
// token值为真,就执行next后面的路由地址
next("/user")
}else{
next("/login")
// 否则就跳转到登录页面
}
}else{
next()
}
})
export default router