2017-01-22 36 views
2

2つの非並行リクエストを実行しようとしていますが、2番目のリクエストを実行する前に最初のリクエストのデータを使用したいと考えています。どのようにして最初のリクエストからデータを取得し、次にそのリクエストを2回目のリクエストで使用することができますか?複数のリクエストを実行する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); 

答えて

4

あなただけの巣最初の内側の二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 
    ); 
    }); 
+0

ありがとうございます!これは完璧に機能します! –

0

また非同期/ 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)); 

OR

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); 
    } 
} 
関連する問題