这里简单记录一些在angular2中使用组件通信的方法。方便您以后使用。1、组件间通信的方式使用事件通信(EventEmitter,@Output):场景:父子组件之间可以通信,一般用于子组件向父组件传递消息;步骤:子组件创建事件EventEmitter对象,使用@output暴露它;b.父组件监听来自子组件@output的方法,然后处理事件。代码://子组件@Component({selector:'app-child',template:'',styles:[``]})exportclassAppChildComponentimplementsOnInit{@Output()onVoted:EventEmitter=newEventEmitter();ngOnInit():void{this.onVoted.emit(1);}}//父组件@Component({selector:'app-parent',template:``,styles:[``]})exportclassAppParentComponentimplementsOnInit{ngOnInit():void{thrownewError('方法未实现。');}onListen(data:any):void{console.log('TAG'+'------------>>>'+data);}}ps:我讨厌贴代码,太占地方了;2、使用@ViewChild和@ViewChildren:场景:一般用于父组件向子组件传递信息,或者父组件调用子组件;步骤:在父组件中使用子组件;b.在父组件中使用@ViewChild获取子组件对象。C。父组件使用子组件对象来操作子组件;(传递信息或调用方法)。代码://子组件@Component({selector:'app-child2',template:'',styles:[``]})exportclassAppChildComponent2implementsOnInit{data=1;ngOnInit():void{}getData():void{console.log('TAG'+'------------>>>'+111);}}//父组件@Component({selector:'app-parent2',template:``,styles:[``]})exportclassAppParentComponent2implementsOnInit{@ViewChild(AppChildComponent2)孩子:AppChildComponent2;ngOnInit():void{this.child.getData();//父组件获取子组件方法console.log('TAG'+'------------>>>'+this.child.data);//父组件获取子组件财产}}3。使用服务Service进行通信,即:两个组件同时注入某个服务:场景:需要通信的两个组件不是父子组件,也不是相邻组件;当然,它们也可以是任何组件。步骤:新建一个服务,组件A和组件B同时注入服务;b.组件A从服务获取数据,或向服务传输数据;C。组件B从服务中获取数据,或向服务传输数据。代码://组件A@Component({selector:'app-a',template:'',styles:[``]})exportclassAppComponentAimplementsOnInit{constructor(privatemessage:MessageService){}ngOnInit():void{//组件A发送消息3this.message.sendMessage(3);constb=this.message.getMessage();//组件A接收到消息;}}//组件B@Component({selector:'app-b',template:``,styles:[``]})exportclassAppComponentBimplementsOnInit{constructor(privatemessage:MessageService){}ngOnInit():void{//组件B获取消息consta=this.message.getMessage();this.message.sendMessage(5);//组件B发送消息}}二、关于我自己的消息服务模块场景:我参与一个项目,需要实现的是所有组件之间可能会相互通信,或者一个组件需要与多个组件通信.设计方法:(1).使用RxJs定义一个服务模块MessageService,所有的信息都注册到服务中;(2).在需要发送消息的地方,调用服务的方法;(3).在需要接收信息的地方,使用,调用接收信息的方法,得到一个Subscription对象,然后监听信息;(4).当然,每个组件Destory的时候,都需要this.subscription.unsubscribe();代码://消息辅助服务@Injectable()exportclassMessageService{privatesubject=newSubject();/***内容模块中的信息传输,类似于广播*@paramtype发送的信息类型*1-你的信息*2-你的信息*3-你的消息*4-你的消息*5-你的消息*/sendMessage(type:number){console.log('TAG'+'------------>>>'+type);this.subject.next({type:type});}/***发送图片信息*@paramsrc:图片地址*/sendImages(src:string){console.log('AG1'+'----------->>>'+src)this.subject.next({url:src});}/***清除消息*/clearMessage(){this.subject.next();}/***获取消息*@returns{Observable}返回消息监听器*/getMessage():Observable{returnthis.subject.asObservable();}}//使用服务的地方,需要注册MessageService服务;constructor(privatemessage:MessageService){}//接收消息的地方;公开订阅:订阅;ngAfterViewInit():void{this.subscription=this.message.getMessage().subscribe(msg=>{//根据msg处理你的业务逻辑})}//当组件生命周期结束时,记得退出,否则会卡住;ngOnDestroy():void{this.subscription.unsubscribe();}//调用服务的方法并发送消息;send():void{this.message.sendImages('www.baidu.com');//发送图片地址this.message.sendMessage(2);//Sendinformationmessage}总结:这里的MessageService相当于使用广播机制在所有组件之间传递信息;无论是数字、字符串还是对象,都可以传输,而且这里的传播速度也非常快。