代码之家  ›  专栏  ›  技术社区  ›  Philipp Meissner

如果添加了其他数据流,解析程序将不解析

  •  2
  • Philipp Meissner  · 技术社区  · 7 年前

    我正试图使用解析器来根据路由持有的给定参数检索数据。

    不幸的是,当我添加另一个数据流时,我的数据依赖于解析器,但实际上从未解析。

    如果我直接返回一个立即解决的值,一切正常。 我调试了情况,看到我收到了所有的部分信息,但最终却没有真正解决。

    这是一个快速样品。如果需要更多的代码来理解这个问题,请给我打电话。

    我的服务:

    export class MyService {
      get(bar) {
        return of(new Foo(bar));
      }
    }
    

    secondservice(此服务从后端检索数据):

    export class SecondService {
      private readonly _observable: Observable<Bar>;
      constructor() {
        this._observable = merge(
          // Other manipulations
        ).pipe(
          // other manipulations
          shareReplay(1)
        )
      }
    
      observable(): Observable<Bar> {
        return this._observable;
      }
    }
    

    分解器:

    export class MyResolver {
      constructor(_secondService: SecondService, _myService: MyService) {}
    
      resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
        // Does not work - Simply does not resolve
        // return this._secondService
        //   .observable()
        //   .pipe(
        //     switchMap((bar) => this._myService.get(bar)),
        //   );
    
        // WORKS
        return of(new Foobar('sampleData'));
      }
    }
    

    路由器:

    const routes: Routes = [
      {
        path: 'someroute',
        component: SomeComponent,
        canActivate: [SomeGuard],
        resolve: {
          myData: MyResolver,
        },
      },
    ];
    

    组件:

    export class SomeComponent implements OnInit {
      constructor(private readonly _route: ActivatedRoute) {}
    
      ngOnInit() {
        this._route.data
          .subscribe((data) => {
          console.log('received:', data);
          this.myData = data;      
        });
      }
    }
    

    somecomponent.html文件

    <pre *ngIf="myData">
      Received: {{ myData | json }}
    </pre>
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Philipp Meissner    7 年前

    take(1) SecondService shareReplay(1)

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
      return this._secondService
        .observable()
        .pipe(
          take(1),
          switchMap((bar) => this._myService.get(bar)),
      );
    }
    

        2
  •  1
  •   eduPeeth    7 年前

    Observable.subscribe()

    export class MyResolver {
      constructor(_secondService: SecondService, _myService: MyService) {}
    
      resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Foo> {
        // Does not work - Simply does not resolve
         return this._secondService
           .observable()
           .pipe(
             take(1),
             switchMap((bar) => this._myService.get(bar)),
           }     
           );
    
        // WORKS
        return of(new Foobar('sampleData')).pipe(
             take(1),
             switchMap((bar) => this._myService.get(bar)),          
         });
      }
    }
    

    推荐文章