Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Filtrar todos los valores 'nulos' de un Observable<T>

Tengo un servicio con un asunto:

 @Injectable() export class UserService() { private currentUserSubject = new BehaviorSubject<User>(null); public currentUser = this.currentUserSubject.asObservable().distinctUntilChanged(); ... // emitting new User }

Tengo un componente en el que inyecto este servicio y me suscribo a las actualizaciones:

 @Component() export class UserComponent { constructor(private userService: UserService) { this.userService.currentUser .subscribe((user) => { // I want to see not null value here }) } }

Quiero aplicar algo a Observable<User> para filtrar todos los valores nulos y suscribirme solo cuando User esté realmente cargado.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Agregue un operador de filtro a su cadena observable. Puede filtrar nulos explícitamente o simplemente verificar que su usuario sea veraz: tendrá que hacer esa llamada según sus necesidades.

Filtrar solo usuarios nulos:

 public currentUser = this.currentUserSubject .asObservable() .filter(user => user !== null) .distinctUntilChanged();
over 4 years ago · Santiago Trujillo Report

0

Existe otra forma de verificar el valor:

 public currentUser = this.currentUserSubject .asObservable() .filter<User>(Boolean) .distinctUntilChanged();
over 4 years ago · Santiago Trujillo Report

0

con rxjs@6 y mecanografiado, la forma recomendada (legible/mantenible) es definir el siguiente tipo de protección:

 export function isNonNull<T>(value: T): value is NonNullable<T> { return value != null; }

y aumentar un sujeto con pipe y filter :

 subject.pipe(filter(isNonNull))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!