如何在路径解析中获得可观察的输出而不是对象

时间:2017-10-12 12:24:15

标签: angular angular2-routing angular-routing

resolve: {course: ResolveService } //这给了一个对象作为回报。而是得到一个可观察的。有可能吗?

export const appRoutes: Routes = [
    { path: 'registration',
      component: SomeComponent,
      canDeactivate: [SomeGuardService],
      resolve: {course: ResolveService } // This gives an object in retrun
     },

// resolve

@Injectable()
export class ResolveService implements Resolve<Observable<any>> {

  constructor(private dropDown: DropDownService) { }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
    return this.dropDown.coursesDt;
  }
}

1 个答案:

答案 0 :(得分:1)

ResolveService中修改resolve以返回可观察的可观察量。 resolve函数将一直等到返回的observable发出,然后将其绑定到route config中定义的值。为了将一个observable发送到route组件,你需要将它包装在另一个observable中。像这样:

@Injectable()
export class ResolveService implements Resolve<Observable<any>> {

    constructor(private dropDown: DropDownService) { }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Observable<any>> {
        return Observable.of(this.dropDown.coursesDt);
    }
}