You can use @Service with a Behaviorsubject. Behaviorsubject contains the value that needs to be shared with other components, it at least is Observer and Observable it can receive and emit the current values.
You will need a Service with an attribute of type Behaviorsubject, in my example I created the same with type number and a default value 0, as below:
@Injectable()
export class QuoteService {
public openQuotes = new BehaviorSubject<number>(0);
setOpenQuote(quotes : number){
this.openQuotes.next(quotes);
}
}
In the Table component will be the place where you take the data, and send it to Behaviorsubject, then you need to inject the Service into the Table component, and in the method called by the green button, you will send the data:
construtor(
quoteService : QuoteService
){}
acaoBotaoVerde(){
this.quoteService.setOpenQuote(quotesOpened);
}
And in the navigation component you will simply subscribe to monitor if the information changes:
construtor(
quoteService : QuoteService
){}
this.quotesOpen = this.quoteService.openQuotes.subscribe(
(quotes) => {
return quotes;
}
);
Sure, this code can be optimized, but it helps you get an idea of what to use.
Have you tried using a Service that has a Behaviorsubject ? In the table component you enter content in Subject, and in the navigation component you give a subscribe in the same... already tried?
– rafaelmacedo
I didn’t know that possibility. How does this work?
– Leonardo Vinicius
I will formulate an answer to exemplify you.
– rafaelmacedo
My opinion: The bar would be in the parent component and the component below would enter through navigation through the
<router-outlet></router-outlet>
. So just use it Eventemitter to notify the parent component whenever something happens in the child component.– Marconi