반응형
여러 요청 수행 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
반응형
'itsource' 카테고리의 다른 글
ByteBuffer.allocate()와ByteBuffer.allocateDirect() (0) | 2022.08.28 |
---|---|
Vuex Store의 작업 내에서 푸시 경로 푸시 (0) | 2022.08.28 |
Vue JS에서 v-for 루프 중에 로컬 변수 할당 (0) | 2022.08.28 |
vueJs에서 filter() 내의 this가 정의되지 않는 이유는 무엇입니까? (0) | 2022.08.28 |
Vue: 컴파일 실패(문자열은 따옴표를 사용해야 함) (0) | 2022.08.28 |