Search code examples
javascriptangularrxjsguard

Supscription in canload guard Angular2+


I'm trying to use combineLatest in canload guard but it gets me this error: Type 'Subscription' is missing the following properties from type 'Observable': _isScalar, source, operator, lift, and 6 more.

my code looks like:

canLoad(route: Route): Observable<boolean> {
        return combineLatest(this.router.events, this.resources$, this.role$)
            .subscribe(([nav, res, role]) => {
              // some logic
                return false;
            })
 }

I want to listen route event and check permissions by route slug. Can someone help me?


Solution

  • You do not have to subscribe just return combineLatest.

    The method except a Observable but you return a Subscription.

    canLoad(route: Route): Observable<boolean> {
            return combineLatest(this.router.events, this.resources$, this.role$)
                   .pipe(
                       // take the first value emitted and complete
                       first()
                       map((value) => {
                           if(value) {
                               return true;
                           }
                           return false;
                       })
                   )
    }