做的產(chǎn)品證書管理系統(tǒng)使用的是VueJs和ElementUI,現(xiàn)將遇到的一些知識點記錄一下。
VUe全局變量的實現(xiàn)
全局變量專用模塊Global.vue是以一個特定模塊來組織管理全局變量,需要引用的地方導(dǎo)入該模塊即可。使用方法如下:
將全局變量模塊掛載到Vue.prototype里,在程序入口的main.js里加下面代碼:
import Global from '../components/Global.vue'Vue.prototype.global = Global
掛載后,在需要引用全局變量的模塊時,不需要再導(dǎo)入全局變量模塊,直接用this引用即可。 如:this.global.notifySuccess()
Vue的全局請求攔截器
在全局變量專用模塊Global.vue中設(shè)置全局Vue請求攔截器,以在全局?jǐn)r截器中添加請求超時的方法為例,若請求超時則取消此次請求,并提示用戶。請求超時設(shè)置通過攔截器Vue.http.interceptors實現(xiàn)具體代碼如下
 Vue.http.interceptors.push((request,next) => {  let timeout  // 如果某個請求設(shè)置了_timeout,那么超過該時間,該終端會(abort)請求,并執(zhí)行請求設(shè)置的鉤子函數(shù)onTimeout方法,不會執(zhí)行then方法。  if (request._timeout) {   timeout = setTimeout(() =>{    if (request.onTimeout) {     request.onTimeout(request)     request.abort()     }    }, request._timeout)  }  next((response) => {   clearTimeout(timeout)   return response   }) })當(dāng)頁面中用到vue-resource請求的地方設(shè)置如下即可:
this.$http.get('url',{    params:{.......},    ......    _timeout:3000,    onTimeout: (request) => {      alert("請求超時");    }  }).then((response)=>{});Vue的全局守衛(wèi)
全局路由守衛(wèi)是指在路由跳轉(zhuǎn)時對登錄狀態(tài)進行檢查。可以使用router.beforeEach注冊一個全局前置守衛(wèi):
const router = new VueRouter({…})Router.beforeEach((to,from,next)=> { …})當(dāng)一個導(dǎo)航觸發(fā)時,全局前置守衛(wèi)按照創(chuàng)建順序調(diào)用。守衛(wèi)是異步解析執(zhí)行,此時導(dǎo)航在所有守衛(wèi)resolve完之前一直處于等待中。每個守衛(wèi)方法接收三個參數(shù):
使用實例如下:
// 全局路由守衛(wèi),路由時檢查用戶是否登錄,若無登錄信息,指向登錄界面router.beforeEach((to, from, next) => {  const nextRoute = ['AdminIndex','系統(tǒng)設(shè)置', '產(chǎn)品管理', '客戶管理', '證書管理', '日志管理']  if (nextRoute.indexOf(to.name)>= 0) {    if (sessionStorage.getItem('username')){      next()    } else {      window.location.replace('/login.html')    }  } else {   next()  }})以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選