Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect if Resolve fails Angular 2

How Can I redirect to another page if the resolve fails in Angular 2? I call this resolve for my Edit Page, but I want to handle the errors in the Resolve Page

My Resolve:

 resolve(route: ActivatedRouteSnapshot): Promise<any>|boolean {          return new Promise((resolve, reject) => {              if (route.params['id'] !== undefined) {                 this.dataService.getHttpEmpresaShow(this.endpoint_url_Get + route.params['id'])                     .subscribe(                      result => {                                                 console.log("ok");                             return resolve(result);                                          },                     error => {                 return resolve(error);             });     }}); 
like image 751
Fernando Herique Rubim Pioli Avatar asked Mar 29 '17 14:03

Fernando Herique Rubim Pioli


1 Answers

Just like in the docs, calling this.router.navigate(["url"])... (think to inject Router in your constructor)

class MyResolve {    constructor(private router: Router) {}    resolve(route: ActivatedRouteSnapshot): Observable <any> {     return this.dataService.getHttpEmpresaShow(this.endpoint_url_Get + route.params['id'])       .pipe(catchError(err => {         this.router.navigate(["/404"]);         return EMPTY;       }));   } } 
like image 106
n00dl3 Avatar answered Sep 27 '22 21:09

n00dl3