반응형
공리에서 "이전" 콜백을 구현하는 방법
난 지금 프로젝트를 쓰고 있다.Vue.js
(사용)axios
파일 업로드 기능 포함.
그 전에 조치를 실행해야 한다.POST
요청이 들어오다axios
:
axios.post('/upload', form, {
before: (xhr) => {
fileObject.xhr = xhr;
},
onUploadProgress: (e) => {
//emit progress event etc...
console.log('upload progress: ' + e.loaded);
}
}).then((response) => {
console.log('finished...');
//emit finished event etc...
}, () => {
console.log('error...');
//emit failed event etc...
});
모든 것이 작동하지만before
물론 콜백(callback)은 안되기 때문이다.axios
옵션의문서를 보면, 요청이 전송되기 전에 인터셉터를 사용하여 후크를 구현해야 한다는 것을 알고 있다.하지만 나는 그것을 극복할 수 없다.
편집: 부에의 것과 비슷한 것으로 하고 싶다.$http
:
this.$http.post('/upload', form, {
before: (xhr) => {
fileObject.xhr = xhr;
//maybe do something else here
},
progress: (e) => {
eventHub.$emit('progress', fileObject, e);
}
}).then((response) => {
eventHub.$emit('finished', fileObject);
}, () => {
eventHub.$emit('failed', fileObject);
})
각 공리가 요청하기 전에 함수를 호출해야 할 경우에는 인터셉터를 사용해야 한다.
귀하의 경우:
axios.interceptors.request.use((config) => {
fileObject.xhr = config;
return config;
});
axios.post('/upload', form, { ... });
참조URL: https://stackoverflow.com/questions/43969324/how-to-implement-a-before-callback-in-axios
반응형
'programing' 카테고리의 다른 글
Vue.js와 함께 dotenv를 사용하는 방법 (0) | 2022.03.10 |
---|---|
bootstrap-vue의 위치 변경 (0) | 2022.03.09 |
기본 반응 보기에 레이아웃 강제 적용 (0) | 2022.03.09 |
형식 지정 함수에 '이것'의 유형을 선언하는 것은? (0) | 2022.03.09 |
웹 팩 개발 서버에서 모든 경로에 대해 index.html을 제공하도록 지시하는 방법 (0) | 2022.03.09 |