路由守卫
- 作用:对路由进行权限控制
- 分类:全局守卫/独享守卫/组件内守卫
- 全局守卫:
router.beforeEach((to, from, next) => {
if (to.meta.isAuth) {
console.log("前置路由守卫");
if (localStorage.getItem("school") === "xueqianduan") {
next();
} else {
alert("学校名称错误,无权限查看");
}
} else {
next();
}
});
router.afterEach((to, from) => {
console.log("后置路由守卫");
document.title = to.meta.title || "默认title";
});
- 独享守卫:
beforeEnter: (to, from, next) => {
if (to.meta.isAuth) {
if (localStorage.getItem("school") === "atguigu") {
next();
} else {
alert("学校名称错误,无权限查看");
}
} else {
next();
}
},
- 组件内守卫:
beforeRouteEnter(to,from,next){..........}
beforeRouteLeave(to,from,next){..........}
路由器的两种工作模式
- 对于一个url来说,什么是hash值?--------#及其后面的内容就是hash值
- hash值不会包含在HTTP请求中,即:hash值不会带给服务器
- hash模式:
- 地址中永远带着#,不美观
- 若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法.
- 兼容性好
- history模式:
- 地址干净,美观
- 兼容性和hash模式相比略差
- 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题