項目需求前端
以上是登陸超時,登陸彈窗框自動彈出來vue
代碼片斷vuex
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, // 須要登陸才能進入的頁面能夠增長一個meta屬性 } } ] ]
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'))); //將用戶信息存儲到vuex中,供全局使用 next(); } else { store.dispatch('initLoginModal', {state: true}); //顯示登陸彈窗 next(); } } else { next(); } }); //攔截http請求中返回401狀態碼,並針對其顯示登陸彈窗 Vue.http.interceptors.push((request, next) => { next((response) => { if (response.body.status === UNAUTHORIZED_CODE) { //與後臺約定登陸失效的返回碼 store.dispatch('initLoginModal', {state: true}); //顯示登陸彈窗 store.dispatch('removeUser'); //移出瀏覽器中存儲的用戶信息 } return response; }); });
以上便能實現登陸以前針對某些特定路由的攔截,和後臺接口受權失效時前端業務邏輯操做處理, 整篇文章僅做知識點積累,若有不妥之處,請多多包涵。瀏覽器