带有Axios Http Get的Rxjs,在请求完成后执行代码

时间:2019-05-28 11:39:15

标签: vue.js rxjs axios

我有一个使用TypeScript,Rxjs和Axios的Vue应用程序。

发出http get请求后,我想执行一些代码,例如隐藏加载指示器。 最好的方法是使用Rxjs? 我知道我在这里订阅数据流。诺言会更好吗?

我目前有以下代码...

Axios.get<Array<account>>(`https://localhost:5003/api/accounts`)
      .pipe(
        concatMap(response => response.data),
        filter(response => filerFunction(response))
      )
      .subscribe(
        response => {this.tableData.push(response); console.log('response: ' + response)},
        error => console.log(error)
      );

1 个答案:

答案 0 :(得分:0)

我已经解决了。您可以使用Finalize运算符或Complete函数...

Axios.get<Array<account>>(`https://localhost:5003/api/accounts`)
  .pipe(
    mergeMap(response => response.data),
    filter(response => filerFunction(response)),
    finalize(()=>{console.log("stop spinner in this function")})
  )
  .subscribe(
    response => this.tableData.push(response),
    error => console.log(error),
    () => console.log("or stop spinner in this function")
  );