typescript 没有 .subscribe 回调的 Angular http.post

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41296052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:06:57  来源:igfitidea点击:

Angular http.post without .subscribe callback

angulartypescriptangular2-services

提问by Vnuuk

I'm wondering if I can make just a http post request without subscribing on callbacks, something like this

我想知道我是否可以只做一个 http post 请求而不订阅回调,像这样

 this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null);

instead of this

而不是这个

 this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null)
        .subscribe();

采纳答案by Picci

I do not think you can.

我不认为你可以。

http.post (and get, put, delete, etc) returns a cold Observable, i.e. an Observable for which:

http.post(以及 get、put、delete 等)返回一个冷 Observable,即一个 Observable:

its underlying producer is created and activated during subscription

它的底层生产者是在订阅期间创建和激活的

Source.

来源

This means the function represented by the Observableis activated only with the subscribe()method.

这意味着由 表示的功能Observable只能通过该subscribe()方法激活。

回答by 2oppin

I'm using conversion to Promise (requires rxjs):

我正在使用转换为 Promise(需要 rxjs):

import 'rxjs/add/operator/toPromise';
@Injectable()
export class SomeService {
....
  post(sp: Seatplace, date?: Date) : Promise<any> {
     return this.http.post(
       '/list/items/update?itemId=' + itemId + "&done=" + done, 
        null
     ).toPromise();
  }
}

回答by lex82

I had the same question but then I figured out that I actually don't care if someone subscribes to the observable. I just want the POST request sent in any case. This is what I came up with:

我有同样的问题,但后来我发现我实际上并不关心是否有人订阅了 observable。我只想在任何情况下发送 POST 请求。这就是我想出的:

postItem(itemData) {
    var observable = this.http.post('/api/items', itemData)
        .map(response => response.json()) // in case you care about returned json       
        .publishReplay(); // would be .publish().replay() in RxJS < v5 I guess
    observable.connect();
    return observable;
}

The request is sent as soon as connect()is called. However, there is still an observable that the caller of postItemcan subscribe to if required. Since publishReplay()is used instead of just publish(), subscribing is possible even after the POST request completed.

请求connect()被调用后立即发送。但是,postItem如果需要,调用者仍然可以订阅一个 observable 。由于publishReplay()被使用而不仅仅是publish(),即使在 POST 请求完成后也可以订阅。

回答by Germán montes

Just like @picci points, regular observables are cold observables. If you want to make the request you can try @lex82 idea, here is a rxjs 6 draft:

就像@picci 点一样,常规的 observables 是冷的 observables。如果你想提出请求,你可以试试@lex82 的想法,这是一个 rxjs 6 草案:

import { ConnectableObservable } from "rxjs"
import { publish } from "rxjs/operators";

const myConnectableObservable: ConnectableObservable<any> = this._http.post('/list/items/update?itemId=' + itemId + "&done=" + done, null).pipe(publish()) as ConnectableObservable<any>;
myConnectableObservable.connect();

which basically is like a subscribe but without to make a real subscription.

这基本上就像订阅,但没有进行真正的订阅。

https://blog.danlew.net/2018/09/25/connectable-observables-so-hot-right-now/

https://blog.danlew.net/2018/09/25/connectable-observables-so-hot-right-now/