1.0.2 • Published 5 years ago
vue-api-eventbus v1.0.2
vue-api-eventbus
Add global axios instance $api
and event bus $eventBus
Installation
npm install vue-api-eventbus
main.js
import VueApiEventbus from 'vue-api-eventbus';
Vue.use(VueApiEventbus[, options]);
options (default)
xhr
axios configurationevents
event list for axios interceptors
options = {
xhr: {
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
baseURL: '/',
},
events: {
beforeRequest: 'before-request',
requestError: 'request-error',
afterResponse: 'after-response',
responseError: 'response-error',
poolCreate: 'pool-create',
poolRemove: 'pool-remove',
poolPush: 'pool-push',
poolPop: 'pool-pop',
}
}
Eventa are triggered:
beforeRequest
: before request createrequestError
: on request errorafterResponse
: after responseresponseError
: on response errorpoolCreate
: generated request poolpoolRemove
: request pool is emptypoolPush
: add new request to the poolpoolPop
: complete request and remove from the pool
Used as
this.$eventBus.$on('event-name', handler);
Usage
Create request
this.$api.get('URL').then((resp) => {})
Listen request lifecycle
this.$eventBus.$on('before-request', handler);
this.$eventBus.$off('before-request', handler);
Custom events
this.$eventBus.$emit('event-name'[, payload]);
this.$eventBus.$on('event-name', handler);
this.$eventBus.$off('event-name', handler);