小编Fre*_*ddy的帖子

Axios:链接多个API请求

我需要从Google Maps API链接一些API请求,而我正在尝试使用Axios.

这是第一个请求,它位于componentWillMount()中

axios.get('https://maps.googleapis.com/maps/api/geocode/json?&address=' + this.props.p1)
  .then(response => this.setState({ p1Location: response.data }))  }
Run Code Online (Sandbox Code Playgroud)

这是第二个请求:

axios.get('https://maps.googleapis.com/maps/api/geocode/json?&address=' + this.props.p2)
  .then(response => this.setState({ p2Location: response.data }))
Run Code Online (Sandbox Code Playgroud)

然后我们有第三个请求,这取决于前两个完成:

axios.get('https://maps.googleapis.com/maps/api/directions/json?origin=place_id:' + this.state.p1Location.results.place_id + '&destination=place_id:' + this.state.p2Location.results.place_id + '&key=' + 'API-KEY-HIDDEN')
  .then(response => this.setState({ route: response.data }))
Run Code Online (Sandbox Code Playgroud)

如何将这三个调用链接起来,以便第三个调用在前两个调用之后发生?

reactjs react-native axios

28
推荐指数
5
解决办法
2万
查看次数

标签 统计

axios ×1

react-native ×1

reactjs ×1