Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Angular component does not receive data from subscribed service when data is loaded from sessionStorage

In my angular app there's a service which receives an object, adds it to a list of objects, saves the updated array of objects to sessionStorage and sends the updated list to another app which is subscribed to changes. The issue is that the subscribed component does not receive data from the service after the user refreshes the page. I see that the service has retrieved the data from the sessionStorage, updates the local array and dispatches the newly loaded data with next to all the subscribers, however the subscribed component does not trigger the subscription.

Now for some code:

    //some.service.ts
      private dataSub = new Subject<Data[]>();
      public dataObservable$ = this.dataSub.asObservable();
      private data: Data[] = [];
    .
    .
    .
      constructor() { 
        const data = sessionStorage.getItem("data");
        if(data){
          this.data = JSON.parse(data);
          this.dataSub.next(this.data);
        }
      }
   .
   .
   .
   public someFunction(){
      sessionStorage.setItem("data", JSON.stringify(this.data));
   }

The component which subscribes to changes in some.service:

//some.component
private dataSubscription: Subscription = new Subscription();
public data: Data[] = [];
constructor(private someService: SomeService) { }
ngOnInit(): void {
   this.dataSubscription = this.someService.dataObservable$.subscribe(newData => {
  this.data= newData ;
 })
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The order in which Angular will construct these classes is Services and then Components.

Since your subject is not replaying values (by default at least) the component will get future values, not current values. This is because the subject was created, added to, and then subscribed to. You can try using a BehaviorSubject or adding a replay to your subject. That way the component will get the current value when it subscribes.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda