导航栏上有首页、智能题库、登陆、退出四个按钮。javascript
用户访问首页时,看到是首页的内容html
1.当用户访问智能题库时,此时要检测用户是否有权限访问该智能题库内容
若是没有:检测用户是否登陆,若是没有登陆则跳转到登陆页面。
登陆完成后,在localStorage中存储该用户的用户名和密码,而且马上跳转到智能题库页面vue
2.当用户点击退出,该用户登陆直接删除。java
<body> <div id="app"></div> <script type="text/javascript" src="vue.js"></script> <!--1.引入vue-router的对象--> <script type="text/javascript" src="./node_modules/vue-router/dist/vue-router.js"></script> <!--全局的VueRouter对象 vue-router还提供了两个全局的组件router-link / router-view--> <script type="text/javascript"> // 2.让Vue使用该VueRouter建立 Vue.use(VueRouter); var Home = { template:` <div> 我是首页 </div> ` }; var Questionbank = { template:` <div> 我是题库 </div> ` }; // 登陆组件 var Login = { data(){ // 数据声明 return { name:'', pwd:'' } }, template:` <div> <input type="text" v-model="name"/> <input type="text" v-model="pwd"/> <input type="button" value="登陆" @click="loginHandler"/> </div> `, methods:{ loginHandler(){ // 要登陆了 alert(1); } } }; // 3.建立一个路由对象 var router = new VueRouter({ // 配置路由对象 routes:[ // 动态路由参数以冒号开头 { path:'/home', component:Home, }, { path:'/questionbank', name:'questionbank', component:Questionbank, }, { path:'/login', name:'login', component:Login } ] }); var App = { // keep-alive组件保持缓存,把router-view渲染的出口缓存起来 template:` <div> <router-link to="/home">首页</router-link> <router-link to="/questionbank">智能题库</router-link> <router-link to="/login">登陆</router-link> <a href="javascript:void(0)" @click="clear">退出</a> <keep-alive> <router-view></router-view> </keep-alive> </div> `, methods:{ clear(){ // 退出 alert(1); } } }; new Vue({ el:'#app', components:{ App }, router, // router:router, 在key和value相同时能够只写一个 template:`<App/>` }); </script> </body>
基本的页面框架已经搭好,后面但愿作到是:在点击智能题库时,判断是否登陆,没有登陆的话跳转到登陆页面登陆,登陆成功后,跳转回智能题库页面显示。node
定义路由的时候能够配置 meta 字段。git
routes
配置中的每一个路由对象为 路由记录。路由记录能够是嵌套的,所以,当一个路由匹配成功后,他可能匹配多个路由记录。github
一个路由匹配到的全部路由记录会暴露为 $route
对象 (还有在导航守卫中的路由对象) 的 $route.matched
数组。所以,咱们须要遍历 $route.matched
来检查路由记录中的 meta
字段。vue-router
// 3.建立一个路由对象 var router = new VueRouter({ // 配置路由对象 routes:[ // 动态路由参数以冒号开头 { path:'/home', component:Home, }, { path:'/questionbank', name:'questionbank', component:Questionbank, // 给将来的路由作权限控制 meta:{ // 若是meta对象的auth为true,代表访问该组件时须要登陆 auth:true } }, { path:'/login', name:'login', component:Login } ] });
必定要调用next,若是不调用next,那么页面会卡住,路由就再也不走了。编程
// 全局的导航守卫(对路由的切换进行守卫) router.beforeEach(function (to, from, next) { console.log(to); console.log(from); // 必定要调用next,若是不调用next,那么页面会卡住,路由再也不走了 if(to.meta.auth){ // 用户点击了智能题库的导航,该用户未登陆,须要登陆判断 if (localStorage.getItem('user')){ // 用户登陆了,不为空,放行 next(); }else{ // 为空进入登陆页面 next({path:'/login'}); } }else { // 没有点击智能题库,直接放行 next(); } });
// 登陆组件 var Login = { data(){ // 数据声明 return { name:'', pwd:'' } }, template:` <div> <input type="text" v-model="name"/> <input type="text" v-model="pwd"/> <input type="button" value="登陆" @click="loginHandler"/> </div> `, methods:{ loginHandler(){ // 要登陆了 // 存储用户名和密码,保存到localStorage,而后跳转到相应的路由(智能导航) localStorage.setItem('user', {name:this.name, pwd:this.pwd}) // 编程式导航 this.$router.push({name:'questionbank'}); } } };
var App = { // keep-alive组件保持缓存,把router-view渲染的出口缓存起来 template:` <div> <router-link to="/home">首页</router-link> <router-link to="/questionbank">智能题库</router-link> <router-link to="/login">登陆</router-link> <a href="javascript:void(0)" @click="clear">退出</a> <keep-alive> <router-view></router-view> </keep-alive> </div> `, methods:{ clear(){ // 退出 localStorage.removeItem('user'); // 清除保存的用户 this.$router.push('/login'); // 跳转到登陆页面 } } };
项目源码查看api
(1)访问页面后,点击智能题库
(2)填写登陆信息登陆
(3)点击登陆后页面跳转
(4)点击退出
vue-router
提供的导航守卫主要用来经过跳转或取消的方式守卫导航。
导航守卫就是路由跳转过程当中的一些钩子函数,再直白点路由跳转是一个大的过程,这个大的过程分为跳转前中后等等细小的过程,在每个过程当中都有一函数,这个函数能让你操做一些其余的事儿的时机,这就是导航守卫。
所以有多种机会植入路由导航过程当中(导航守卫分类):全局的、单个路由独享的、组件级的。
1.全局前置守卫:beforeEach
2.路由beforeEnter守卫
3.组件路由守卫beforeRouteEnter,此时this并不指向该组件实例
4.全局解析守卫:beforeResolve
5.全局后置守卫:afterEach
6.组件生命周期:beforeCreate
7.组件生命周期:created
8.组件生命周期:beforeMount
9.组件生命周期:mounted
10.组件路由守卫beforeRouteEnter的next回调
to: Route
: 即将要进入的目标 路由对象
from: Route
: 当前导航正要离开的路由
next: Function
: 必定要调用该方法来 resolve 这个钩子。执行效果依赖 next
方法的调用参数。
next()
: 进行管道中的下一个钩子。若是所有钩子执行完了,则导航的状态就是 confirmed (确认的)。
next(false)
: 中断当前的导航。若是浏览器的 URL 改变了 (多是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from
路由对应的地址。
next('/')
或者 next({ path: '/' })
: 跳转到一个不一样的地址。当前的导航被中断,而后进行一个新的导航。你能够向 next
传递任意位置对象,且容许设置诸如 replace: true
、name: 'home'
之类的选项以及任何用在 router-link
的 to
prop 或 router.push
中的选项。
next(error)
: (2.4.0+) 若是传入 next
的参数是一个 Error
实例,则导航会被终止且该错误会被传递给 router.onError()
注册过的回调。
全局路由守卫指路由实例上直接操做的钩子函数,他的特色是全部路由配置的组件都会触发,直白点就是触发路由就会触发这些钩子函数,以下的写法。钩子函数按执行顺序包括beforeEach、beforeResolve(2.5+)、afterEach三个。
在路由跳转前触发,参数包括to,from,next三个,这个钩子主要用于登陆验证,也就是路由还没跳转提早告知,以避免跳转了再通知就为时已晚。
const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { // ... })
当一个导航触发时,全局前置守卫按照建立顺序调用。守卫是异步解析执行,此时导航在全部守卫 resolve 完以前一直处于 等待中。
和beforeEach相似,也是路由跳转前触发,参数也是to,from,next三个。
区别在于:导航被确认以前,同时在全部组件内守卫和异步路由组件被解析以后,解析守卫就被调用。
和beforeEach相反,他是在路由跳转完成后触发,参数包括to,from没有了next,这些钩子不会接受 next
函数也不会改变导航自己。
router.afterEach((to, from) => { // ... })
指在单个路由配置的时候也能够设置的钩子函数,其位置就是下面示例中的位置,也就是像Foo这样的组件都存在这样的钩子函数。
const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... } } ] })
这些守卫与全局前置守卫的方法参数是一致的。
指在组件内执行的钩子函数,相似于组件内的生命周期,至关于为配置路由的组件添加的生命周期钩子函数。钩子函数按执行顺序包括beforeRouteEnter、beforeRouteUpdate (2.2+)、beforeRouteLeave三个,执行位置以下:
<template> ... </template> export default{ data(){ //... }, beforeRouteEnter (to, from, next) { // 在渲染该组件的对应路由被 confirm 前调用 // 不!能!获取组件实例 `this` // 由于当守卫执行前,组件实例还没被建立 }, beforeRouteUpdate (to, from, next) { // 在当前路由改变,可是该组件被复用时调用 // 举例来讲,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候, // 因为会渲染一样的 Foo 组件,所以组件实例会被复用。而这个钩子就会在这个状况下被调用。 // 能够访问组件实例 `this` }, beforeRouteLeave (to, from, next) { // 导航离开该组件的对应路由时调用 // 能够访问组件实例 `this` } }
注意:
beforeRouteEnter
守卫不能访问 this
,由于守卫在导航确认前被调用,所以即将登场的新组件还没被建立。不过,你能够经过传一个回调给 next
来访问组件实例。在导航被确认的时候执行回调,而且把组件实例做为回调方法的参数。
beforeRouteEnter (to, from, next) { next(vm => { // 经过 `vm` 访问组件实例 }) }
beforeRouteEnter
是支持给 next
传递回调的惟一守卫。对于 beforeRouteUpdate
和 beforeRouteLeave
来讲,this
已经可用了,因此不支持传递回调,由于没有必要了。
beforeRouteUpdate (to, from, next) { // just use `this` this.name = to.params.name next() }
离开守卫一般用来禁止用户在还未保存修改前忽然离开。该导航能够经过 next(false)
来取消。
beforeRouteLeave (to, from , next) { const answer = window.confirm('Do you really want to leave? you have unsaved changes!') if (answer) { next() } else { next(false) } }
beforeEach
守卫。beforeRouteUpdate
守卫 (2.2+)。beforeEnter
。beforeRouteEnter
。beforeResolve
守卫 (2.5+)。afterEach
钩子。beforeRouteEnter
守卫中传给 next
的回调函数。