作的产品证书管理系统使用的是VueJs和ElementUI,现将遇到的一些知识点记录一下。html
全局变量专用模块Global.vue是以一个特定模块来组织管理全局变量,须要引用的地方导入该模块便可。使用方法以下:
将全局变量模块挂载到Vue.prototype里,在程序入口的main.js里加下面代码:vue
import Global from '../components/Global.vue' Vue.prototype.global = Global
挂载后,在须要引用全局变量的模块时,不须要再导入全局变量模块,直接用this引用便可。 如:this.global.notifySuccess()session
在全局变量专用模块Global.vue中设置全局Vue请求拦截器,以在全局拦截器中添加请求超时的方法为例,若请求超时则取消这次请求,并提示用户。请求超时设置经过拦截器Vue.http.interceptors实现具体代码以下:异步
Vue.http.interceptors.push((request,next) => { let timeout // 若是某个请求设置了_timeout,那么超过该时间,该终端会(abort)请求,并执行请求设置的钩子函数onTimeout方法,不会执行then方法。 if (request._timeout) { timeout = setTimeout(() =>{ if (request.onTimeout) { request.onTimeout(request) request.abort() } }, request._timeout) } next((response) => { clearTimeout(timeout) return response }) })
当页面中用到vue-resource请求的地方设置以下便可:函数
this.$http.get('url',{ params:{.......}, ...... _timeout:3000, onTimeout: (request) => { alert("请求超时"); } }).then((response)=>{ });
全局路由守卫是指在路由跳转时对登陆状态进行检查。能够使用router.beforeEach注册一个全局前置守卫:vue-resource
const router = new VueRouter({…}) Router.beforeEach((to,from,next)=> { …})
当一个导航触发时,全局前置守卫按照建立顺序调用。守卫是异步解析执行,此时导航在全部守卫resolve完以前一直处于等待中。每一个守卫方法接收三个参数:
to:Route即将要进入的目标,即路由对象;
from:Route当前导航正要离开的路由;
next:Function:必定要调用该方法来resolve这个钩子。执行效果依赖next方法的调用参数。
使用实例以下:this
// 全局路由守卫,路由时检查用户是否登陆,若无登陆信息,指向登陆界面 router.beforeEach((to, from, next) => { const nextRoute = ['AdminIndex','系统设置', '产品管理', '客户管理', '证书管理', '日志管理'] if (nextRoute.indexOf(to.name)>= 0) { if (sessionStorage.getItem('username')){ next() } else { window.location.replace('/login.html') } } else { next() } })