Возврат разрешенных данных из Pipe Angular 4 с использованием объекта поведения - PullRequest
0 голосов
/ 05 марта 2019

Мой сервис: 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 голосов
/ 05 марта 2019

Решил это.Причина, по которой ResolveEnd не запускался, была в том, что я пропустил `this.tranDetailSubject.complete ();в Сведения об отправке () сразу после следующего ().

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.

});
}

`

...