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

时间:2018-02-23 10:40:04

标签: angular post ionic-framework request

在父组件中

我得到了未定义的数据值。 这是我的代码。

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});
}

1 个答案:

答案 0 :(得分:0)

像这样使用

返回this.http.post(&#34; http://localhost:8080/hello&#34;,{&#39; q&#39;:ques},{header:header}) .map(data =&gt; {console.log(data);}) .map((resp:Response)=&gt; {return resp;});

它会有所帮助