简体   繁体   中英

Wait for an observable to resolve in Angular

<ng-container *ngIf="user | async; else login"> 
    // redirect to home page
</ng-container>

<ng-template #login>
   // display login form
</ng-template>

How to wait for 'user' observable to resolve, before 'login' form is displayed?

You can add something like this to your template:

<ng-container *ngIf="$loaded | async; else loading"> 
  <ng-container *ngIf="$user | async; else login"> 
      // redirect to home page
  </ng-container>

  <ng-template #login>
    // display login form
  </ng-template>
</ng-container>

<ng-template #loading>
   // loading
</ng-template>

And in the component's code:

  $loaded = this.$user.pipe(
    mapTo(true),
    startWith(false)
  );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM