路由守卫,将限制访问路由的权限。
前置路由守卫,每次路由切换前将被调用。
后置路由守卫,每次路由切换后将被调用。
独享路由守卫,由某个路由独享,在路由切换前调用。
全局前置路由守卫
全局前置路由守卫,即router.beforeEach( (to,from,next) => {} )
,注意三个参数:to、from和next。
- 一般组件components/Banner.vue
<template>
<div class="col-xs-offset-2 col-xs-8">
<div class="page-header">
<h2>Vue Router Demo</h2>
</div>
</div>
</template>
<script>
export default {
name:"Banner"
}
</script>
<style>
</style>
- 路由组件pages/Home.vue
<template>
<div>
<h2>Home组件内容</h2>
</div>
</template>
<script>
export default {
name:'Home',
}
</script>
<style>
</style>
- 路由组件pages/About.vue
<template>
<h2>我是About的内容</h2>
</template>
<script>
export default {
name:'About'
}
</script>
<style>
</style>
- 路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
const router = new VueRouter({
routes:[
{
name:"guanyu",
path:"/about",
component:About
},
{
name:"zhuye",
path:"/home",
component:Home
}
]
})
router.beforeEach((to,from,next) => {
if(to.name == "zhuye"){
if(localStorage.getItem("company") === "五哈科技有限公司"){
next();
}else{
alert("公司名不正确!")
}
}else{
next();
}
})
export default router;
或者也可以这样
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
const router = new VueRouter({
routes:[
{
path:"/about",
component:About
},
{
path:"/home",
component:Home,
meta:{
isAuth:true
}
}
]
})
router.beforeEach((to,from,next) => {
if(to.meta.isAuth){
if(localStorage.getItem("company") === "五哈科技有限公司"){
next();
}else{
alert("公司名不正确!")
}
}else{
next();
}
})
export default router;
- App.vue
<template>
<div>
<div class="row">
<Banner/>
</div>
<div class="row">
<div class="col-xs-2 col-xs-offset-2">
<div class="list-group">
<router-link to="/about" active-class="active" class="list-group-item">About</router-link>
<router-link to="/home" active-class="active" class="list-group-item">Home</router-link>
</div>
</div>
<div class="col-xs-6">
<div class="panel">
<div class="panel-body">
<keep-alive>
<router-view></router-view>
</keep-alive>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Banner from "./components/Banner.vue";
export default {
name: 'App',
components:{
Banner
}
}
</script>
- 入口文件main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router";
import router from "./router/index";
Vue.config.productionTip = false;
Vue.use(VueRouter);
new Vue({
render: h => h(App),
router:router
}).$mount('#app')
- 启动应用,测试效果。
全局后置路由守卫
全局后置路由守卫,即router.afterEach( (to,from) => {} )
,注意两个参数:to、from。
- 修改路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
const router = new VueRouter({
routes:[
{
path:"/about",
component:About,
meta:{
title:"关于"
}
},
{
path:"/home",
component:Home,
meta:{
isAuth:true,
title:"首页"
}
}
]
})
router.beforeEach((to,from,next) => {
if(to.meta.isAuth){
if(localStorage.getItem("company") === "五哈科技有限公司"){
next();
}else{
alert("公司名不正确!")
}
}else{
next();
}
})
router.afterEach((to,from) => {
document.title = to.meta.title || "嘎嘎嘎官网";
})
export default router;
- 启动应用,测试效果。
独享路由守卫
独享路由守卫,即 beforeEnter( (to,from,next) => {} )
,注意三个参数:to、from、next。
- 修改路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
const router = new VueRouter({
routes:[
{
path:"/about",
component:About,
meta:{
title:"关于"
}
},
{
path:"/home",
component:Home,
meta:{
title:"首页"
},
beforeEnter(to,from,next){
if(localStorage.getItem("company") === "五和科技有限公司"){
next();
}else{
alert("公司名不正确!");
}
}
}
]
})
router.afterEach((to,from) => {
document.title = to.meta.title || "嘎嘎嘎官网";
})
export default router;
- 重启应用,测试效果。
组件内路由守卫
组件内路由守卫,有
进入路由守卫:beforeRouteEnter(to,from,next){}
。根据路由规则,进入组件时调用。
离开路由守卫:beforeRouteLeave(to,from,next){}
。根据路由规则,离开组件时调用。
- 修改路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
const router = new VueRouter({
routes:[
{
path:"/about",
component:About,
meta:{
title:"关于"
}
},
{
path:"/home",
component:Home,
meta:{
title:"首页",
isAuth:true
}
}
]
})
router.afterEach((to,from) => {
document.title = to.meta.title || "嘎嘎嘎官网";
})
export default router;
- 修改路由组件Home.vue
<template>
<div>
<h2>Home组件内容</h2>
</div>
</template>
<script>
export default {
name:'Home',
beforeRouteEnter (to, from, next) {
console.log("Home#beforeRouteEnter");
if(to.meta.isAuth){
if(localStorage.getItem("company") === "五哈科技有限公司"){
next();
}else{
alert("公司名不正确!");
}
}else{
next();
}
},
beforeRouteLeave (to, from, next) {
console.log("Home#beforeRouteLeave");
next();
}
}
</script>
<style>
</style>
- 重启应用,测试效果。