Angular2 http.post执行两次 [英] Angular2 http.post gets executed twice

查看:239
本文介绍了Angular2 http.post执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,Angular2的(RC1)Http服务执行两次http.post调用。我调试了我的应用程序,我知道这不是点击事件问题。导致核心服务调用的所有调用

I came across a weird issue where the Angular2's (RC1) Http service executes the http.post call twice. I've debugged my app and I know for a fact this is not a click event issue. All the calls that lead up to the core service call

public create(json: Object, params?: Object): Observable<T> {
    let body = JSON.stringify([json]);
    let headers = this.getHeaders();
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this.createURL(this.getCreateURL(), [], params), body, options)
    .map(res => this.handleObjectResponse(res));
}

运行一次。然后当我开始跟踪问题时,我发现我的处理程序 this.handleObjectResponse 被执行两次。所以我进一步钻研并达到 @ angular / http / src / backends / xhr_backend.ts 他们这样做了

are run once. Then when I started tracing the issue I found out that my handler this.handleObjectResponse gets executed twice. So I delved further and reached @angular/http/src/backends/xhr_backend.ts where they do this

constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
    this.request = req;
    this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
        let _xhr: XMLHttpRequest = browserXHR.build();
        _xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
        // load event handler
        ...
        ..

所以我在 this.request = req; 上设置断点,然后在<$ c $上设置另一个断点c>让_xhr:XMLHttpRequest = browserXHR.build(); 然后我发现我遇到了第一个断点,但后来我从回调中击中了第二个断点两次。

So I put a breakpoint on this.request = req; and then another breakpoint on let _xhr: XMLHttpRequest = browserXHR.build(); and I found out I hit the first breakpoint once but then I hit the second breakpoint from the callback twice.

这让我疯了,所以我想检查是否有人熟悉angular2内部装置是否可以解释这是否是一个错误或者我做错了什么。

This has been driving me nuts so I wanted to check whether anyone familiar with the angular2 internals could shed some light whether this looks like a bug or something that I've done wrong.

在我的代码中,我创建了一些抽象的通用服务类:GenericService和FullService,它扩展了GenericService。这些都是抽象的并且使用泛型s和在不同组件中注入的实际服务类都扩展了GenericService或FullService。你们认为这个设置可能是双重执行后的责任吗?

In my code I've created some abstract generic service classes: GenericService and FullService which extends GenericService. Both of these are abstract and use generics and the real service classes that get injected in the different components all extend either GenericService or FullService. Do you guys think this setup could possibly be responsible for the double post executions?

所有的想法都很受欢迎!

All ideas are appreciated!

提前致谢!

PS

这不会发生在获取但是也会发生。

This doesn't happen with gets but it also happens with puts.

推荐答案

http服务返回一个冷的observable来获取在每个订阅上执行,您希望将其转换为仅在第一个订阅时执行的热点可观察对象,并为后续订阅共享相同的值。

The http service returns a cold observable that get executed on every subscribe, you want to convert it to a hot observable that get only executed on the first subscribe and share the same value for subsequent subscribes.

要转换它,你所要做的就是分享它:

To convert it all you have to do is share it:

return this._http.post(this.createURL(this.getCreateURL(), [], params), body, options)
.map(res => this.handleObjectResponse(res))
.share();

这篇关于Angular2 http.post执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆