您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用vue-resource請(qǐng)求怎么實(shí)現(xiàn)http登錄攔截,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
路由文件
export default [ { name: 'root', path: '', component: function (resolve) { require(['你的vue文件路徑地址'], resolve); }, children: [ { name: 'applicationLayout', path: '/app/applicationLayout/:appId', component: function (resolve) { require(['你的vue文件路徑地址'], resolve); }, meta: { requireAuth: true, // 需要登錄才能進(jìn)入的頁(yè)面可以增加一個(gè)meta屬性 } } ] ]
main.js 入口文件中加入以下代碼
const UNAUTHORIZED_CODE = 401; router.beforeEach((to, from, next) => { if (to.meta.requireAuth) { if (storage.get('platformUser')) { store.dispatch('loginUser', JSON.parse(storage.get('platformUser'))); //將用戶(hù)信息存儲(chǔ)到vuex中,供全局使用 next(); } else { store.dispatch('initLoginModal', {state: true}); //顯示登錄彈窗 next(); } } else { next(); } }); //攔截http請(qǐng)求中返回401狀態(tài)碼,并針對(duì)其顯示登錄彈窗 Vue.http.interceptors.push((request, next) => { next((response) => { if (response.body.status === UNAUTHORIZED_CODE) { //與后臺(tái)約定登錄失效的返回碼 store.dispatch('initLoginModal', {state: true}); //顯示登錄彈窗 store.dispatch('removeUser'); //移出瀏覽器中存儲(chǔ)的用戶(hù)信息 } return response; }); });
上述內(nèi)容就是使用vue-resource請(qǐng)求怎么實(shí)現(xiàn)http登錄攔截,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。