這篇文章將為大家詳細(xì)講解有關(guān)利用angular怎么實(shí)現(xiàn)不同的組件間傳值與通信,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)代縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
父子組件間參數(shù)與通訊方法
使用事件通信(EventEmitter,@Output):
場(chǎng)景:可以在父子組件之間進(jìn)行通信,一般使用在子組件傳遞消息給父組件;
步驟:
子組件創(chuàng)建事件EventEmitter對(duì)象,使用@output公開出去;
父組件監(jiān)聽子組件@output出來的方法,然后處理事件。
代碼:
// 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);
}
}使用@ViewChild和@ViewChildren:
場(chǎng)景:一般用于父組件給子組件傳遞信息,或者父組件調(diào)用子組件的方法;
步驟:
父組件里面使用子組件;
父組件里面使用@ViewChild獲得子組件對(duì)象。
父組件使用子組件對(duì)象操控子組件;(傳遞信息或者調(diào)用方法)。
代碼:
// 子組件
@Component({
selector: 'app-child',
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);// 父組件獲得子組件屬性
}
}非父子組件參數(shù)傳遞與通訊方法
通過路由參數(shù)
場(chǎng)景:一個(gè)組件可以通過路由的方式跳轉(zhuǎn)到另一個(gè)組件 如:列表與編輯
步驟:
A組件通過routerLink或router.navigate或router.navigateByUrl進(jìn)行頁(yè)面跳轉(zhuǎn)到B組件
B組件接受這些參數(shù)
此方法只適用于參數(shù)傳遞,組件間的參數(shù)一旦接收就不會(huì)變化
代碼
傳遞方式
routerLink
<a routerLink=["/exampledetail",id]></a>
routerLink=["/exampledetail",{queryParams:object}]
routerLink=["/exampledetail",{queryParams:'id':'1','name':'yxman'}];router.navigate
this.router.navigate(['/exampledetail',id]);
this.router.navigate(['/exampledetail'],{queryParams:{'name':'yxman'}});router.navigateByUrl
this.router.navigateByUrl('/exampledetail/id');
this.router.navigateByUrl('/exampledetail',{queryParams:{'name':'yxman'}});傳參方傳參之后,接收方2種接收方式如下:
snapshot
import { ActivateRoute } from '@angular/router';
public data: any;
export class ExampledetailComponent implements OnInit {
constructor( public route: ActivateRoute ) { };
ngOnInit(){
this.data = this.route.snapshot.params['id'];
};
}queryParams
import { ActivateRoute } from '@angular/router';
export class ExampledetailComponent implements OnInit {
public data: any;
constructor( public activeRoute:ActivateRoute ) { };
ngOnInit(){
this.activeRoute.queryParams.subscribe(params => {
this.data = params['name'];
});
};使用服務(wù)Service進(jìn)行通信,即:兩個(gè)組件同時(shí)注入某個(gè)服務(wù)
場(chǎng)景:需要通信的兩個(gè)組件不是父子組件或者不是相鄰組件;當(dāng)然,也可以是任意組件。
步驟:
新建一個(gè)服務(wù),組件A和組件B同時(shí)注入該服務(wù);
組件A從服務(wù)獲得數(shù)據(jù),或者想服務(wù)傳輸數(shù)據(jù)
組件B從服務(wù)獲得數(shù)據(jù),或者想服務(wù)傳輸數(shù)據(jù)。
代碼:
// 組件A
@Component({
selector: 'app-a',
template: '',
styles: [``]
})
export class AppComponentA implements OnInit {
constructor(private message: MessageService) {
}
ngOnInit(): void {
// 組件A發(fā)送消息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發(fā)送消息
}
}消息服務(wù)模塊
場(chǎng)景:這里涉及到一個(gè)項(xiàng)目,里面需要實(shí)現(xiàn)的是所有組件之間都有可能通信,或者是一個(gè)組件需要給幾個(gè)組件通信,且不可通過路由進(jìn)行傳參。
設(shè)計(jì)方式:
使用RxJs,定義一個(gè)服務(wù)模塊MessageService,所有的信息都注冊(cè)該服務(wù);
需要發(fā)消息的地方,調(diào)用該服務(wù)的方法;
需要接受信息的地方使用,調(diào)用接受信息的方法,獲得一個(gè)Subscription對(duì)象,然后監(jiān)聽信息;
當(dāng)然,在每一個(gè)組件Destory的時(shí)候,需要
this.subscription.unsubscribe();
代碼:
// 消息中專服務(wù)
@Injectable()
export class MessageService {
private subject = new Subject<any>();
/**
* content模塊里面進(jìn)行信息傳輸,類似廣播
* @param type 發(fā)送的信息類型
* 1-你的信息
* 2-你的信息
* 3-你的信息
* 4-你的信息
* 5-你的信息
*/
sendMessage(type: number) {
console.log('TAG' + '---------->>>' + type);
this.subject.next({type: type});
}
/**
* 清理消息
*/
clearMessage() {
this.subject.next();
}
/**
* 獲得消息
* @returns {Observable<any>} 返回消息監(jiān)聽
*/
getMessage(): Observable<any> {
return this.subject.asObservable();
}
}
// 使用該服務(wù)的地方,需要注冊(cè)MessageService服務(wù);
constructor(private message: MessageService) {
}
// 消息接受的地方;
public subscription: Subscription;
ngAfterViewInit(): void {
this.subscription = this.message.getMessage().subscribe(msg => {
// 根據(jù)msg,來處理你的業(yè)務(wù)邏輯。
})
}
// 組件生命周期結(jié)束的時(shí)候,記得注銷一下,不然會(huì)卡;
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
// 調(diào)用該服務(wù)的方法,發(fā)送信息;
send():void {
this.message.sendMessage(‘我發(fā)消息了,你們接受下'); // 發(fā)送信息消息
}這里的MessageService,就相當(dāng)于使用廣播機(jī)制,在所有的組件之間傳遞信息;不管是數(shù)字,字符串,還是對(duì)象都是可以傳遞的,而且這里的傳播速度也是很快的。
關(guān)于利用angular怎么實(shí)現(xiàn)不同的組件間傳值與通信就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:利用angular怎么實(shí)現(xiàn)不同的組件間傳值與通信
網(wǎng)站網(wǎng)址:http://chinadenli.net/article12/gghjgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、域名注冊(cè)、小程序開發(fā)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)