Cancel Route Navigation from Resolver in Angular 2
In order to cancel route from resolver, you can use observable instead of promise and call: observer.complete() without calling: observer.next()
For example:
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
return new Observable((observer) => {
let somePromiseOrObservableToGetDate=?????;
somePromiseOrObservableToGetDate.then(data => {
if (data) {
observer.next(1);
}
observer.complete();
})
.catch((err: any) => {
observer.complete();
});
});
}
and in your example:
let model: MyModel = new MyModel();
return new Observable((observer) => {
model.init().then((obj: any) => {
observer.next(obj);
observer.complete();
})
.catch((err: any) => {
observer.complete();
});
});