使用行为主题从Pipe Angular 4返回解析的数据

时间:2019-03-05 04:29:55

标签: angular typescript http angular-routing angular-services

我的服务是: DetailService.ts

发送详细信息()

sendDetail(id: number) {
    console.log("Snd trandetail");

    const url = this.rootUrl + 'api/Details/Select?ID=' + id;

    this.http.get(url).pipe(
      retry (3)
    ).toPromise()
    .then((data: any) => {
        this.detailSubject.next(data.Entity);
    });
}

GetDetail()

getDetail() {
    console.log("Get trandetail");
    console.log(this.detailSubject);
    return this.detailSubject;
}

我的解析器是:

Resolver.ts

resolve(route:ActivatedRouteSnapshot, state:RouterStateSnapshot): Observable<any> {
    return this.DetailService.getDetail()
        .pipe(
            map(object => 
            {
                console.log(object); //Data is fetched  here and printed
                return object;           
            })
        );
}

路由到子组件:

{
  path: 'edit/:state',
  component: DetailComponent,
  data: {
    text: 'edit',
    nav: true,
    breadcrumbs: true
  },

  resolve: {
    object: Resolver
  },
  canActivate: [AuthGuard]

},

 providers: [ Resolver, DetailService ]

路由到父模块:

 {
      path: 'detailsModule',
      loadChildren: 'app/layout/Details/some- 
 details/some-details.module#SomeDetailsModule',
      data: {
          preload: false,
          text: 'trans Amendment'
       },
       canActivate: [AuthGuard]
 },

问题: 我的路线似乎无法导航到该组件。如果启用跟踪,则会发现未触发 ResolveEnd 。 这就是我在组件处调用服务的方式:

ngOnInit() {
  console.log("Object from Route");
  console.log(this.route.snapshot.data['object']);   
  this.object = this.route.snapshot.data['object'];
}

我要去哪里错了?任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

解决了。未触发ResolveEnd的原因是因为我错过了`this.tranDetailSubject.complete();。在发送详细信息()之后,紧接着next()。

SendDetail():

sendDetail(id: number) {
console.log("Snd trandetail");

const url = this.rootUrl + 'api/Details/Select?ID=' + id;

this.http.get(url).pipe(
  retry (3)
).toPromise()
.then((data: any) => {
    this.detailSubject.next(data.Entity);
    this.detailSubject.complete(); //Tells compiler to trigger ResolveEnd because observable is complete now.

});
}

`