如何在角度4中将变量值从一个组件传递到另一组件

时间:2018-09-25 04:10:50

标签: angular

export class Component{

 constructor(){}

    if (
  this.categoryId != undefined &&
  (this.serviceId == undefined || this.serviceId == 0)
) {
  if (this.locationName == undefined) {
    alert("call by category id API");
    this.postService.searchListingById(this.categoryId).subscribe(res => {
      console.log(res);
      this.rawdata = res.json();
      console.log(this.rawdata);
    });
    this.router.navigate(["/web/search"]);
  } else {
    alert("Call by Category Location Api");
   }
 }
}
  

我在 this.rawdata 中存储了一个值,我想将该值传递给另一个组件,该如何使用角度4实现它

     

我有这样的rawdata值

(6) [{…}, {…}, {…}, {…}, {…}, {…}]
 0: {id: 16, installerTitle: "Test71", firstName: "Test71", middleName: 
"Test71", lastName: "Test71", …}
 1: {id: 3, installerTitle: "Test19", firstName: "Test19", middleName: 
"Test19", lastName: "Test19", …}
2: {id: 3, installerTitle: "Test19", firstName: "Test19", middleName: 
"Test19", lastName: "Test19", …}
3: {id: 57, installerTitle: "Listing Title", firstName: "Pankaj", 
middleName: "Kumar", lastName: "Sharma", …}
4: {id: 58, installerTitle: "Test128", firstName: "Test128", middleName: 
"Test128", lastName: "Test128", …}
5: {id: 58, installerTitle: "Test128", firstName: "Test128", middleName: 
"Test128", lastName: "Test128", …}

1 个答案:

答案 0 :(得分:0)

最好的解决方案是使用服务。喜欢这篇文章:

https://angularfirebase.com/lessons/sharing-data-between-angular-components-four-methods/

部分:不相关的组件:与服务共享数据

请查看所有选项。