I needed to cancel the route on promise rejection and stay on the current page without redirecting to default/error page, I have tried different ways of rejecting the promise but ended up resolving the route or redirected to the default route.
@Injectable()
export class ModelResolver implements Resolve<MyModel> {
constructor(private router: Router) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {
let params: any = route.params;
let model: MyModel= new MyModel();
return model.init().then((obj: MyModel) => {
return obj;
}).catch((err: any) => {
Promise.reject({message: err , ngNavigationCancelingError: true});
});
}
}
reject({message: err , ngNavigationCancelingError: true});
// redirects to my default route
return Observable.throw({message: err , ngNavigationCancelingError: true});
// redirects to the current route without cancelling
resolve(null); or return Observable.empty()
// redirects to the current route without cancelling