Angular2基于搜索结果路由到页面

时间:2017-02-23 15:57:50

标签: angular

如果搜索结果只有一个项目(如果reportId = 1),我想导航到不同的页面,用户可以保存一次点击。这是我的解析器代码。

export class SearchResultsResolver implements Resolve<Report[]> {
        constructor(private searchService: SearchService, private router: Router) {}

    resolve(route: ActivatedRouteSnapshot,
         state: RouterStateSnapshot): Observable<Report[]> {
        let reportId = route.params['reportId'];

        return this.searchService.getReports(reportId);

        console.log('reportid', reportId);
    }
}

1 个答案:

答案 0 :(得分:0)

您只需使用flatMapObserver.ifrouter.navigate的组合即可完成,如下面的代码段所示

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Array<Report>> {
  let reportId = route.params['reportId'];

  return this.searchService.getReports(reportId)
 .flatMap(reports => Observable.if(() => reports && reports.length > 1, 
      Observable.of(reports),
      this._router.navigate(['/otherRoute']))
  );