2017-02-23 19 views
0

如果搜索結果只有一個項目(如果reportId = 1),我想要導航到不同的頁面(如果reportId = 1),用戶可以保存一次點擊。這是我的解析器代碼。Angular2根據搜索結果路由到頁面

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); 
    } 
} 

回答

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'])) 
);