简体   繁体   English

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

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

in parent component i got undefined value for data. 在父组件中,我获得了数据的不确定值。 here is my code. 这是我的代码。

parentComponent.ts 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 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});
}

use like this 这样使用

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

it will helps 这会有所帮助

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

相关问题 Angular4 @Input()将值从父组件传递到子组件 - Angular4 @Input() to pass value from parent component to child component 如何在Angular4中将数据从子组件传递到父组件 - How to pass data from child component to parent component in Angular4 如何通过更改Angular4中子组件中的值来更新父组件中数组的长度? - How can I update length of array in parent component from changing value in child component in Angular4? 将数据从子组件传递到父组件Angular4 - pass data from child component to parent component Angular4 Angular4如何在确保父对象组件变量未定义之后从父组件变量中为其分配变量 - Angular4 how to assign variable in child component from parent component variable after reassuring that it is not undefined 如何将值从服务传递到父组件 - How to pass value from a service to a parent component 在Angular4组件中使用离子本机服务 - Use ionic native service in Angular4 component 无法在angular4中将数据从父组件传递到子组件 - Not able to pass data from parent to child component in angular4 如何在Angular 2中将值从父组件传递给指令? - how can I pass a value from parent component to directive in Angular 2? 如何在特定输入标签下方的子组件angular4中传递并显示服务中的验证错误 - How to pass and show validation error from service in child component angular4 below particular input tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM