GuillaumeC
GuillaumeC

Reputation: 555

Angular2: Return an object in a resolver

I have to make a check in my resolver, if it's fine, I reassign the object, otherwise, I return an observable with the value false.

Here is my code:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.callApi().subscribe((result) => {
        if (blabla === true) {
            return Observable.of(result);
        }
        this.router.navigate([ '/' ]);
        return Observable.of(false);
    });
}

The Observable.of(false) seems to work, I go to the homepage, but with Observable.of(result) , I don't get the result in my component ...

Thank you for your help

Upvotes: 2

Views: 1030

Answers (1)

Meir
Meir

Reputation: 14375

You don't even need switchMap, you have the result available. Note that you convert both result and false to observables, but you can return them directly.

return this.callApi().map(result => (blahblah ? result : false));

The side effect inside the caller is not a best practice, you can handle it outside:

resolve(...).filter(result => !result).subscribe(() => this.router.navigate([ '/' ]));

Upvotes: 1

Related Questions