没有 .subscribe 回调的 Angular http.post

标签 angular typescript angular2-services

我想知道我是否可以在不订阅回调的情况下只发出一个 http post 请求,就像这样

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

而不是这个

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

最佳答案

我认为你做不到。

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

its underlying producer is created and activated during subscription

Source .

这意味着由 Observable 表示的函数仅通过 subscribe() 方法激活。

便捷方法也可以订阅,请参阅Observable#toPromise() 的实现细节 here .

关于没有 .subscribe 回调的 Angular http.post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296052/

相关文章:

javascript - 在运行时向控制台方法添加动态值,并完整保留原始调用位置和行号

angular - 需要在函数中访问服务

angularjs - Angular 2 : Injecting a provider into a component using a custom decorator or annotation?

angular - 在变换管道中使用链式可观察量

dependency-injection - Angular 2 中的依赖注入(inject)

angular - Ag-Grid 编辑数据并发送到服务器

javascript - 内容丰富 + Angular2

angular - 如何减轻标签点击?

javascript - 为什么我会收到 createIndex 弃用错误?

javascript - Angular 2 : How to call a function after get a response from subscribe http. 后