is there an equivalent of async pipe that you can use inside a component?

前端 未结 4 937
离开以前
离开以前 2021-02-13 03:52

Does there exist something equivalent to the async pipe that I could use inside a component like this

   @Component({
      selector: \'my-component         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-13 04:31

    If you're in a Component or something which has access to a ChangeDetectorRef, you can do the following little trick:

    @Component({
    ...
      providers: [AsyncPipe]
    })
    export class ExampleComponent {
      app$ = this.sandbox.allApps$;
      apps = this.pipeAsync.transform(this.app$);
    
      contructor(
        protected pipeAsync: AsyncPipe,
        protected sandbox: AppAppsSandbox
      ) {}
    }
    

    This assumes access to a service called AppAppsSandbox, but the Observable app$ can come from anywhere, this is a convenient way to unwrap it.

提交回复
热议问题