我在代码中遇到了一个小问题,这让我有点困惑,希望有人能解释为什么它会这样做。
Code 1
sendText(){
return this.http.get('/api')
.map((response:Response) => response.json());
}
Code 2
sendText(){
return this.http.get('/api').map((response:Response) => {
response.json();
});
}
这两个代码之间的主要区别在于Code 2我将括号放在箭头函数后面,以将我的任务添加到这些括号内并在Code 1我把括号去掉,把任务放在一行上。
我的问题是为什么来自服务器端的对象返回为未定义Code2使用 angular2 提供的 subscribe 方法Code1返回我怀疑的对象。
(response:Response) => response.json()
This is 速记为了这:
(response:Response) => { return response.json(); }
The {}
让你添加multiple块内的语句。如果没有它们,该函数将仅运行一条语句并返回其值。
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)