How to perform a function on one component and affect another component - Angular 6

Asked

Viewed 2,064 times

1

I would like to know how I can through one component affect another component at angular 6. These components are not father and son. They are distinct.

<app-navegacao></app-navegacao>
<router-outlet></router-outlet>

Where is Open/Closed Quotes is my navigation bar: ""

And where the table is my other component that is part of a router-outlet:

Imagem 1

I want to run the event associated with the green button and send the values to my navigation bar

  • 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?

  • I didn’t know that possibility. How does this work?

  • I will formulate an answer to exemplify you.

  • 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.

2 answers

1

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.

  • Thanks bro. I’ll test you here. If you’re not going to abuse your goodwill.. I would like to know if you have a material where I can study rxjs and how to know if a request has been made and whether or not this request has worked. Starting now to develop in front and back and this is getting in my way.

  • Friend confess that the content in PT-Br is very scarce, but for rxjs more specifically the Observables, I recommend this article here, in it you will see about the return methods, then, error and etc... https://tableless.com.br/understanding/

-2

Below I will pass a very simple example of how to shoot and listen to events from different contexts, will be described what to do in each context.

CONTEXT - Green button component service:

import { EventEmitter } from '@angular/core'; //import necessario

public eventoEmit = new EventEmitter<boolean>(); // seu emissor de evento

public Click_BotaoVerde()
{
    //demais ações 
    this.eventoEmit.emit(true); // emitindo o evento
}

CONTEXT - Component menu

constructor(private _btnVerdeService: BtnVerdeService){

  this._btnVerdeService.eventoEmit.subscribe(x => {
      // Aqui você já esta ouvindo os eventos do btn verde
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.