這裏簡單的記錄本身在angular2中,使用組件通訊的一些方法。方便本身之後的使用。angular2
1、組件之間通訊的方式app
// child 組件 @Component({ selector: 'app-child', template: '', styles: [``] }) export class AppChildComponent implements OnInit { @Output() onVoted: EventEmitter<any> = new EventEmitter(); ngOnInit(): void { this.onVoted.emit(1); } } // parent 組件 @Component({ selector: 'app-parent', template: ` <app-child (onVoted)="onListen($event)"></app-child> `, styles: [``] }) export class AppParentComponent implements OnInit { ngOnInit(): void { throw new Error('Method not implemented.'); } onListen(data: any): void { console.log('TAG' + '---------->>>' + data); } } ps:很討厭貼代碼,太佔空間了;
2.使用@ViewChild和@ViewChildren:this
// 子組件 @Component({ selector: 'app-child2', template: '', styles: [``] }) export class AppChildComponent2 implements OnInit { data = 1; ngOnInit(): void { } getData(): void { console.log('TAG' + '---------->>>' + 111); } }
// 父組件 @Component({ selector: 'app-parent2', template: ` <app-child></app-child> `, styles: [``] }) export class AppParentComponent2 implements OnInit { @ViewChild(AppChildComponent2) child: AppChildComponent2; ngOnInit(): void { this.child.getData(); // 父組件得到子組件方法 console.log('TAG'+'---------->>>'+this.child.data);// 父組件得到子組件屬性 } }
3.使用服務Service進行通訊,即:兩個組件同時注入某個服務:url
// 組件A @Component({ selector: 'app-a', template: '', styles: [``] }) export class AppComponentA implements OnInit { constructor(private message: MessageService) { } ngOnInit(): void { // 組件A發送消息3 this.message.sendMessage(3); const b = this.message.getMessage(); // 組件A接收消息; } }
// 組件B @Component({ selector: 'app-b', template: ` <app-a></app-a> `, styles: [``] }) export class AppComponentB implements OnInit { constructor(private message: MessageService) { } ngOnInit(): void { // 組件B得到消息 const a = this.message.getMessage(); this.message.sendMessage(5); // 組件B發送消息 } }
2、關於我本身的消息服務模塊設計
// 消息中專服務 @Injectable() export class MessageService { private subject = new Subject<any>(); /** * content模塊裏面進行信息傳輸,相似廣播 * @param type 發送的信息類型 * 1-你的信息 * 2-你的信息 * 3-你的信息 * 4-你的信息 * 5-你的信息 */ sendMessage(type: number) { console.log('TAG' + '---------->>>' + type); this.subject.next({type: type}); } /** * 發送圖片信息 * @param src:圖片地址 */ sendImages(src: string) { console.log('AG1' + '---------->>>' + src) this.subject.next({url: src}); } /** * 清理消息 */ clearMessage() { this.subject.next(); } /** * 得到消息 * @returns {Observable<any>} 返回消息監聽 */ getMessage(): Observable<any> { return this.subject.asObservable(); } }
// 使用該服務的地方,須要註冊MessageService服務; constructor(private message: MessageService) { } // 消息接受的地方; public subscription: Subscription; 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); // 發送信息消息 }
總結:這裏的MessageService,就至關於使用廣播機制,在全部的組件之間傳遞信息;無論是數字,字符串,仍是對象都是能夠傳遞的,並且這裏的傳播速度也是很快的。code