programing

공리에서 "이전" 콜백을 구현하는 방법

prostudy 2022. 3. 9. 10:39
반응형

공리에서 "이전" 콜백을 구현하는 방법

난 지금 프로젝트를 쓰고 있다.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

반응형