itsource

여러 요청 수행 Axios(Vue.js)

mycopycode 2022. 8. 28. 09:55
반응형

여러 요청 수행 Axios(Vue.js)

두 개의 비동시 요청을 수행하려고 하는데, 두 번째 요청을 수행하기 전에 첫 번째 요청의 데이터를 사용하고 싶습니다.첫 번째 요청에서 데이터를 가져온 후 두 번째 요청에 해당 데이터를 사용하려면 어떻게 해야 합니까?

axios.get('/user/12345').then(response => this.arrayOne = response.data);
axios.get('/user/12345/' + this.arrayOne.name + '/permissions').then(response => this.arrayTwo = response.data);

두 번째를 둥지로 옮기면 돼axios첫 번째 놈에게 전화해요

axios.get('/user/12345').then(response => {
   this.arrayOne = response.data
   axios.get('/user/12345/' + this.arrayOne.name + '/permissions').then(
       response => this.arrayTwo = response.data
     );
  });

ES2017에서는 비동기/대기 기능을 사용할 수도 있습니다.

myFunction = async () => {
  const response1 = await axios.get('/user/12345');
  this.arrayOne = response1.data;
  const response2 = await axios.get(`/user/12345/${this.arrayOne.name}/permissions`);
  this.arrayTwo = response2.data;
}
myFunction().catch(e => console.log(e));

또는

myFunction = async () => {
  try {
    const response1 = await axios.get('/user/12345');
    this.arrayOne = response1.data;
    const response2 = await axios.get(`/user/12345/${this.arrayOne.name}/permissions`);
    this.arrayTwo = response2.data;
  } catch(e) {
    console.log(e);
  }
}

언급URL : https://stackoverflow.com/questions/41787994/performing-multiple-requests-axios-vue-js

반응형