全局路由守卫
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
//路由表
const routes = [
// 重定向定的是Home页面
{
path: "/",
redirect: "/Home"
},
{
path: "/Login",
name: "Login",
component: () => import("@/components/Login.vue") //对应的组件模板
},
{
path: "/Home", //链接路径
name: "Home",
component: () => import("@/components/Home.vue") //对应的组件模板
}
];
const router = new VueRouter({
routes //路由表 必写的
});
//路由守卫
router.beforeEach((to, form, next) => {
const nextRoute = ["Home"];
// const auth = store.state.auth;
// console.log(to.name);
//未登录
if (nextRoute.indexOf(to.name) >= 0) {
//未登录
// console.log(localStorage.getItem("name"));
let name = localStorage.getItem("name");
if (name == null) {
router.push({ name: "Login" });
}
}
// 已登录状态;当路由到login时,跳转至home
if (to.name === "Login") {
let name = localStorage.getItem("name");
if (name != null) {
router.push({ name: "Home" });
}
}
next();
});
//导出router
export default router;
组件守卫
beforeRouteEnter:进入组件时
beforeRouteUpdate:路由不变,传递的参数改变
beforeRouteLeave: 离开组件前
//进入该路由时判断如果有token 如果没有token,转到登录页面
beforeRouteEnter:(to,from,next)=>{
var tokens=getCookie("token");
if(tokens.length>0){
next()
}else{
alert("请您先登录")
next('/login')
}
},
路由独享守卫
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// 参数用法什么的都一样,调用顺序在全局前置守卫后面,所以不会被全局守卫覆盖
// ...
}
}
]
})
一石激起千层浪,继中国区浩浩荡荡的大裁员告一段落之后,甲骨文并未因此收起手...
微信文件传输助手是微信电脑版与手机微信之间相互传输图片等文件的好工具,但很...
ADO对象: Connection Command Recordset Record Stream ASP支持的对象很多,可...
【排序算法】之lowb三人组冒泡、插入、选择 什么是lowb三人组 冒泡排序bubble so...
本文将研究 ES6 的 for ... of 循环。 旧方法 在过去,有两种方法可以遍历 javas...
计算属性computed: 支持缓存,只有依赖数据发生改变,才会重新进行计算 不支持...
一、正则表达式概述 二、正则表达式在VBScript中的应用 三、正则表达式在VavaScr...
歌词编辑器 歌词编辑器 第一步:选择要播放的歌曲并播放 第二步:填写全部的歌词...
前言 相信大家都知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也...
vbs:把一段文字中指定字符颜色变成红色的正则 functionc(Tstr,Word) Dimre Setre...