Answer the question
In order to leave comments, you need to log in
How to put Vue.http.interceptors in a separate file?
I need to set http interceptors. Now they are set in my main.js file. But to fence everything there, it’s not right and not convenient, I want to transfer the interceptors to a separate file. Those. do like this.
import {Loading} from 'element-ui';
import {LOADING_CONFIG} from './const';
import Vue from 'vue';
const addLoadingOnRequest = (request, next) => {
const loading = Loading.service(LOADING_CONFIG);
next(() => {
loading.close();
});
};
const showErrorMessageOnFailedRequest = (request, next) => {
next(response => {
if (response.status > 308) {
alert('API Error');
}
})
};
Vue.http.interceptors.push(addLoadingOnRequest);
Vue.http.interceptors.push(showErrorMessageOnFailedRequest);
Answer the question
In order to leave comments, you need to log in
Move only the interceptors to the interceptors.js file:
import {Loading} from 'element-ui';
import {LOADING_CONFIG} from './const';
import Vue from 'vue';
export const addLoadingOnRequest = (request, next) => {
const loading = Loading.service(LOADING_CONFIG);
next(() => {
loading.close();
});
};
export const showErrorMessageOnFailedRequest = (request, next) => {
next(response => {
if (response.status > 308) {
alert('API Error');
}
})
};
import {addLoadingOnRequest, showErrorMessageOnFailedRequest} from 'interceptors';
Vue.http.interceptors.push(addLoadingOnRequest);
Vue.http.interceptors.push(showErrorMessageOnFailedRequest);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question