Angular 6 組件之間的數(shù)據(jù)通信

基本情況有3種讶隐,父子組件,兄弟組件久又,任意組件的數(shù)據(jù)通信巫延。

父組件向子組件傳值時,在子組件中使用Input 地消,通過這種方式將模板傳遞給子組件烈评。

情形一:父組件傳值到子組件;

父組件
import {component} from "@angular/core";

@Component({
selector:'app-parent',
template:`<app-child   [child]="message"><app-child>`犯建,
styleurls:['./parent.component.css']
}),

export class ParentComponent{
public  message=" 父組件的數(shù)據(jù) ";
constructor(){}
}
子組件
import {component,Input} from "@angular/core";

@Component({
selector:'app-child',
template:`這是接受  {{message}}`讲冠,
styleurls:['./child.component.css']
}),

export class ParentComponent{
@Input() child:string;
constructor(){}
}

情形二: 子組件傳值到父組件
第一種方法:
使用viewChild

  父組件
import {component,viewChild, AfterViewInit} from "@angular/core";
import {ChildComponent} from "./child/child.component";
@Component({
selector:'app-parent',
template:`父組件 {{message}}  <app-child><app-child>`适瓦,
styleurls:['./parent.component.css']
}),

export class ParentComponent implements AfterViewInit{
@viewChild(ChildComponent) child;

public  message:string;
constructor(){}

ngAfterViewInit(){
this.message=this.child.message;
}

子組件
import {component,Input} from "@angular/core";

@Component({
selector:'app-child',
template:``竿开,
styleurls:['./child.component.css']
}),

export class ParentComponent{
public child="我是子組件";
constructor(){}
}

第二種:使用Output的方式emit廣播出去。


父組件
import {component} from "@angular/core";

@Component({
selector:'app-parent',
template:`接受來自子組件的數(shù)據(jù):{{message}}  <app-child (messageEvent)="getMessage($event)"><app-child>`玻熙,
styleurls:['./parent.component.css']
}),

export class ParentComponent{
public  message:string;
constructor(){}
}

//接受子組件傳遞的值
getMessage(event){
this.message=event
}

子組件
import {component,Output否彩,EventEmitter} from "@angular/core";

@Component({
selector:'app-child',
template:`<button (click)="send()">發(fā)送 </button>`,
styleurls:['./child.component.css']
}),

export class ParentComponent{
public child="我是子組件";

@Output() messageEvent=new EventEmitter<string>()
constructor(){}
}
//發(fā)送子組件的值
send():void{
  this.messageEvent.emit(this.message)
}

情形三:非父子時的情況
創(chuàng)建一個共享服務(wù)嗦随,通過Rxjs中的BehaviorSubject 來存儲數(shù)據(jù)列荔。這樣每個組件通過訂閱這個數(shù)據(jù),當(dāng)這個數(shù)據(jù)發(fā)生變化的時候枚尼,都可以獲取最新的數(shù)據(jù)贴浙,rxjs中有個四個訂閱方式,適用不同的場景
ReplaySubject ,AsyncSubject, Subject

創(chuàng)建一個服務(wù)


import {Injectable } from "@angular/core";
import{BehaviorSubject }from "rxjs";

export class DataService{
public currentMessage=new BehaviorSubject<string>('默認(rèn)數(shù)據(jù)')署恍;
constructor(){}

changeMessage(message:string):void{
this.currentMessage.next(message)
}

}

組件1

組件1
import {component} from "@angular/core";
import {DataService } from "./service/data.service"
@Component({
selector:'app-parent',
template:`接受來自組件1的數(shù)據(jù):{{message}}  <a`崎溃,
styleurls:['./parent.component.css']
}),

export class ParentComponent{
public  message:string;
constructor( private data :DataService){

this.data.currentMessage.subscribe(res=>this.message=res)
}
}



組件2


組件
import {component} from "@angular/core";
import {DataService } from "./service/data.service"
Component({
selector:'app-child',
template:` {{message}}<button (click)="send()">發(fā)送 服務(wù)組件</button>`,
styleurls:['./child.component.css']
}),

export class ParentComponent implements onInit{

public message:string
@Output() messageEvent=new EventEmitter<string>()
constructor(private data:DataService){}
ngOnint(){
//通過服務(wù)接收數(shù)據(jù)
this.data.current.subscribe(res=>this.message=res)
}
//通過服務(wù)發(fā)送數(shù)據(jù)
send():void{
  this.data.changeMessage('再次發(fā)送數(shù)據(jù)盯质!')
}

參考網(wǎng)址:https://ninesix.cc/post/sharing/sharing-between-angular-component.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末袁串,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子呼巷,更是在濱河造成了極大的恐慌囱修,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件王悍,死亡現(xiàn)場離奇詭異破镰,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門啤咽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人渠脉,你說我怎么就攤上這事宇整。” “怎么了芋膘?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵鳞青,是天一觀的道長。 經(jīng)常有香客問我为朋,道長臂拓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任习寸,我火速辦了婚禮胶惰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘霞溪。我一直安慰自己孵滞,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布鸯匹。 她就那樣靜靜地躺著坊饶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪殴蓬。 梳的紋絲不亂的頭發(fā)上匿级,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音染厅,去河邊找鬼痘绎。 笑死,一個胖子當(dāng)著我的面吹牛肖粮,可吹牛的內(nèi)容都是我干的简逮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尿赚,長吁一口氣:“原來是場噩夢啊……” “哼散庶!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起凌净,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤悲龟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后冰寻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體须教,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了轻腺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乐疆。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖贬养,靈堂內(nèi)的尸體忽然破棺而出挤土,到底是詐尸還是另有隱情,我是刑警寧澤误算,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布仰美,位于F島的核電站,受9級特大地震影響儿礼,放射性物質(zhì)發(fā)生泄漏咖杂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一蚊夫、第九天 我趴在偏房一處隱蔽的房頂上張望诉字。 院中可真熱鬧,春花似錦知纷、人聲如沸奏窑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽埃唯。三九已至,卻和暖如春鹰晨,著一層夾襖步出監(jiān)牢的瞬間墨叛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工模蜡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留漠趁,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓忍疾,卻偏偏與公主長得像闯传,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子卤妒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容