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

查看:31
本文介绍了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; 上设置了一个断点,然后在 let _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.这两个都是抽象的并使用泛型,注入到不同组件中的真实服务类都扩展了 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?

感谢所有想法!

提前致谢!

附言

gets 不会发生这种情况,但 puts 也会发生这种情况.

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.

要转换它,您只需分享:

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

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

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