打字稿代码应该调用 POST 端点。终点只是一个没有正文参数的网址。我已经通过提供 url 而没有其他参数来尝试 http.post() 方法。
示例代码。
let postUrl = "http://hostname:9893/service/fetch/72637";
this.http.post(postUrl, {}).map((res:Response) => res.json()).subscribe( data => {
console.log("post url: " + data );
});
错误:
core.es5.js:1020 message:"Cannot read property 'post' of undefined" stack:"TypeError: Cannot read property 'post' of undefined\n at HTMLTableRowElement.<anonymous> (http://localhost:3000/5.chunk.js:222:23)\n at HTMLTableRowElement.wrapFn [as __zone_symbol___onclick] (http://localhost:3000/polyfills.bundle.js:6601:39)\n at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:5969:31)\n at Object.onInvokeTask (http://localhost:3000/vendor.bundle.js:66347:33)\n at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:3000/polyfills.bundle.js:5968:36)\n at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (http://localhost:3000/polyfills.bundle.js:5736:47)\n at HTMLTableRowElement.ZoneTask.invoke (http://localhost:3000/polyfills.bundle.js:6031:38)"
__proto__:Error {constructor: , name: "TypeError", message: "", …} constructor:function TypeError() { … }
更新:
解决方案:
上面的代码在回调中使用,所以这个上下文被更新为不同的上下文,因此 http 不可用。与此绑定解决了该问题。
上面的代码在回调中使用,所以这个上下文被更新为不同的上下文,因此 http 不可用。与此绑定解决了该问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句