uksz
uksz

Reputation: 18719

Is there a way to navigate to other view from CanActivate?

I have create following annotation:

@CanActivate(
(next,prev)=> {
    var neededPermission = ["PERMISSION"];
    var userPermissions = JSON.parse(sessionStorage.getItem('permissions'));
    if (userPermissions && !_.isEmpty(_.intersection(userPermissions, neededPermission))) {
        return true
    }else {
//Navigate to No Permission state?
        return false;
    }
}

)

And I am wondering how I can navigate user to other page, if the result of the funcition is false?

Upvotes: 1

Views: 90

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657536

The Plunker linked to in this comment shows how this can be done

The Protected component has a @CanActivate(...) decorator that calls isLoggedIn() which calls router.navigate().

This Plunker demonstrates how to use DI in @CanActivate()

Upvotes: 1

Related Questions