this.eventsService.broadcast('something', "Am I a?", "Should be b", "C?");
this.eventsService.on('something', function (a, b, c) {
console.log(a, b, c);
});
< a href = " https://plnkr.co/edit/EjxffJLwTAusMI6BtEUw?p=preview" rel="noreferrer">活塞示例:Message Service . p=preview" rel="noreferrer">活塞示例:Message Service . p=preview" rel="noreferrer">
< a href = " https://plnkr.co/edit/EjxffJLwTAusMI6BtEUw?p=preview" rel="noreferrer">活塞示例:Message Service . p=preview" rel="noreferrer">活塞示例:Message Service . p=preview" rel="noreferrer">
export Class myService {
#all the stuff that must exist
myString: string[] = [];
contactChange : BehaviorSubject<string[]> = new BehaviorSubject(this.myString);
getContacts(newContacts) {
// get your data from a webservices & when you done simply next the value
this.contactChange.next(newContacts);
}
}
export Class myComp {
#all the stuff that exists like @Component + constructor using (private myService: myService)
this.myService.contactChange.subscribe((contacts) => {
this.contactList += contacts; //run everytime next is called
}
}