繁体   English   中英

如何从angular4(ionic3)的服务提供者组件传递响应给父组件,我得到了未定义的值?

[英]how to pass a response to parent component from service provider component of angular4 (ionic3), i got undefined value?

在父组件中,我获得了数据的不确定值。 这是我的代码。

parentComponent.ts

testMethod(){
  console.log('t', this.answer.postRequest(this.quetion).subscribe(data => { 
    console.log('d',data);
  })); //here i got undefined value to data
}

serviceProvider.ts

postRequest(ques:string):Observable<any>{
  let header = new HttpHeaders({
    'Content-Type' : 'application/json',"Accept":'application/json'
  });
  console.log(ques)

  return this.http.post(
    "http://localhost:8080/hello",
    {'q': ques},
    {headers: header}
  ).map(data => {
    console.log(data);
  }, // this work fine and value is assigned to data as expected
  error => {(error);// Error getting the data});
}

这样使用

返回this.http.post(“ http:// localhost:8080 / hello ”,{'q':ques},{headers:header}).map(data => {console.log(data);})。 map((resp:Response)=> {return resp;});

这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM