Angular Cli:如何阻止服务器发送连续请求?

时间:2018-04-19 17:02:06

标签: angular angular-cli

如何在宣布之前的结果之前停止第二个请求?在 Angular Cli

例如引号

这是我的 quotes.component.ts:

let data =[
  ["51.9435","-4.26697","450","125"],
  ["51.9437","-4.26717","450","125"],
  ["51.9438","-4.26733","450","125"],
  ["51.944","-4.26748","450","125"]
];

let removeIndex = (array, index) => array.map(a => (a.splice(index, 1), a));

console.log(removeIndex(data, 2));

如您所见,我设置了“ busy = false; ”并将“ if ”设置为

  

如果 busy == false ,请运行代码   但是当我发送另一个请求时,两次或更多次......服务器接受所有请求

如何在宣布之前的结果之前停止第二个请求?

1 个答案:

答案 0 :(得分:0)

假设QuoteService.getQuotes()返回rxjs/Observablesubscribe方法具有以下签名:Observable.subscribe([successCallback, errorCallback, completeCallback])。行this.loading = false; this.busy = false;既不是successCallback也不是errorCallback的一部分;它们会立即执行,您实际上将四个参数传递给subscribe

扩展两个回调中的代码以包含两行,如下所示:

this.quoteService.getQuotes().subscribe(
    (quotes:Quote[]) => {
        this.quotes = quotes;
        this.loading = false;
        this.busy = false;
    }, 

    (error: Response) => {
        console.log(error);
        this.loading = false;
        this.busy = false;
    });

或者,更好的是,使用finally

this.quoteService.getQuotes()
.finally(() => {
    this.loading = false;
    this.busy = false;
})
.subscribe(
    (quotes:Quote[]) => this.quotes = quotes, 
    (error: Response) => console.log(error)
);