[英]Typescript Angular2 - complaining about missing types
I have the following code: 我有以下代码:
var headers = new Headers();
// headers.append('Content-Type', 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://192.168.1.45:3000/testrestapi',
{headers: headers}
).map((res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
console.log('Friends Provider was a success!');
console.log(JSON.stringify(data));
resolve(this.data);
},
(err)=>{
console.log('Error in Friends Provider!');
},
()=>{
console.log('Friends Provider network call has ended!');
}
)
)
});
The code compiles fine without error, but I get the following errors in my ide: 代码可以正常编译,没有错误,但是我的想法却出现以下错误:
I am following this docs: https://angular.io/docs/js/latest/api/http/Http-class.html . 我正在关注以下文档: https : //angular.io/docs/js/latest/api/http/Http-class.html 。 It shows how to use HTTP for GET, but not for POST.
它显示了如何将HTTP用于GET,而不用于POST。 It seems I am missing the type for my headers which I am unsure what to put here as well it complains about the type for the subscribe method and again I am unsure what to put here as I am following the docs and it does not seem to have anything different?
似乎我缺少标头的类型,我不确定要放在这里的内容,也抱怨subscribe方法的类型,而且我不确定在关注文档时要放在这里的内容,而且似乎也没有有什么不同吗?
You have imported Http module 2 times in 2nd and 3rd lines of your code, one of them is redundant. 您已经在代码的第二行和第三行中两次导入了Http模块,其中之一是多余的。
API for http post is: http发布的API是:
post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>
in https://angular.io/api/http/Http 在https://angular.io/api/http/Http中
it expects a JSON.stringified body as the second parameter but you are pushing the header options which should be the third parameter. 它期望将JSON.stringified主体作为第二个参数,但是您正在推送应作为第三个参数的标头选项。
I also suggest using 我也建议使用
headers.set('Content-Type', 'application/json');
instead of append
. 而不是
append
。
Also you are missing the paranthesis in subscribe after data. 另外,您在数据后订阅中缺少偏见。
.subscribe(
(data) => {
console.log('Do something with data here \n',data);
}
);
var headers = new Headers();
// headers.append('Content-Type', 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(
'http://192.168.1.45:3000/testrestapi',
{headers: headers}
)
.map((res => res.json())
.subscribe(data =>
{
this.data = data;
console.log('Friends Provider was a success!');
console.log(JSON.stringify(data));
//resolve(this.data);
},
(err)=>{
console.log('Error in Friends Provider!');
},
()=>{
console.log('Friends Provider network call has ended!');
}
);
The code compiles fine without error,
代码编译良好,没有错误,
Just because you get valid JavaScript doesn't mean that it compiled without error. 仅仅因为您获得了有效的JavaScript ,并不意味着它就正确编译了。 This is actually a TypeScript feature (see why typescript ).
这实际上是TypeScript 功能 (请参阅为什么使用typescript )。
The errors you are seeing in the error are the compiler errrors 您在错误中看到的错误是编译器错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.