Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
How to use @ngrx/store in component?

My understanding after reading the documentation is that in order to obtain values from an NgRx store, we have to use selectors. So far, I've had luck doing that, only with a small issue regarding typing, which makes me worried if my implementation is incorrect in any way.

So, let's say I want to retrieve a number value from the store, id for example. In my componentName.component.ts I would access this value like this:

id$ = this.store.select(selectId);

where selectId is defined from another file as:

export const selectData = (state: AppState) => state.data;

export const selectId = createSelector(
  selectData,
  (state: DataState) => state.id,
)

I am able to access id$ in my HTML component easily by doing {{id$ | async}}, but accessing it from the component class itself has proven a bit more difficult. For starters, the variable type is Observable<number> instead of just number, and that makes it hard to use it in cases where it needs to be of number type, such as when comparing it:

  ngOnInit(): void {
    console.log(this.id$ === 0);
  }

The TypeScript error I get from the code above is:

TS2367: This condition will always return 'false' since the types 'number' and 'Observable ' have no overlap.

And console-logging id$ itself confirms that it is indeed of Observable type, so this leads me to believe I am doing something wrong, but I'm unsure exactly what. Any help is appreciated!!!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

id$ is an Observable , and you can access its value by subscribing from the component class using the subscribe function, or from the component template using an async pipeline.

In component class:

 ngOnInit(subscribe): void { this.id$.subscribe(id => console.log(id)); }

In component template:

 <span>{{ id$ | async }}</span>

See the official Angular docs about Observable (s):

https://angular.io/guide/observables

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda