Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 用于数据交换/使用的两个不同组件之间的通信:角度2_Angular_Typescript - Fatal编程技术网

Angular 用于数据交换/使用的两个不同组件之间的通信:角度2

Angular 用于数据交换/使用的两个不同组件之间的通信:角度2,angular,typescript,Angular,Typescript,这是我的观点部分: 客户组件正在从服务器获取客户数据和地址数据 客户组件 export class CustomerComponent extends SuperChildComponent { public static url: string = 'orderViewCustomer'; public id; public allowed: boolean = false; public accessLevel: AccessLevel = null; public

这是我的观点部分:


客户组件正在从服务器获取客户数据和地址数据

客户组件

export class CustomerComponent extends SuperChildComponent {
  public static url: string = 'orderViewCustomer';
  public id;
  public allowed: boolean = false;
  public accessLevel: AccessLevel = null;
  public componentname: string;
  public customerData: Customer = null;

  constructor(private rest: REST, private authenticationService: AuthorizationService) {
    super();
    this.componentname = this.constructor.name;
    this.accessLevel = this.authenticationService.isUserLoggedIn() ? this.authenticationService.componentAccessLevel(this.constructor.name) : null;
    console.log(this.constructor.name + ' has ' + this.accessLevel);
    if (this.accessLevel == AccessLevel.ENABLED) {
      this.getData();
    }
  }

  private getData(): any {
     this.rest.get(CustomerComponent.url,this.id).subscribe(data=> this.storeData(data.json()), err => console.log(err) ,()=> this.accessLevel==AccessLevel.DISBLED);

  }



  private storeData(res: Object): any {
    this.customerData =<Customer>res;

  }


}
class Customer {
  id: string = null;
  oldId: string = "";
  firstName: string = "";
  lastName: string = "";
  email: string = "";
  addressLine1: string = "";
  addressLine2: string = "";
  addressLine3: string = "";
  street1: string = "";
  street2: string = "";
  postalCode: string = "";
  city: string = "";
  title: string = "";
  gender: string = "";

}
问题:我不确定如何将数据从客户组件传递到地址组件

我可以将客户组件注入地址组件的构造函数吗


请提供您的意见。谢谢

我想您正在寻找@input和@output装饰器;以及EventEmitter类。 在中了解更多有关它的信息


您也可以通过共享服务来实现这一点。

我认为您正在寻找@input和@output装饰器;以及EventEmitter类。 在中了解更多有关它的信息


您也可以使用共享服务来实现这一点。

最简单的方法是在父组件上提供服务,并将其注入到
CustomerComponent
AddressComponent
中,并使用可观察项进行通信。最简单的方法是在父组件上提供服务,并将其注入到
CustomerComponent
寻址组件
并使用可观察对象进行通信
export class AddressComponent implements OnInit {

 public customer:CustomerComponent;
 public addressData;
  constructor() { 
    this.addressData=this.customer.customerData;

  }