kamal soni
kamal soni

Reputation: 71

CanActivate route guard is not getting invoked

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
import { Observable } from 'rxjs/Observable';
    
@Injectable()
export class AuthGuardService implements CanActivate {
    
  constructor() { }
        
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
    console.log("can activate called");
    return true;
  }
}

// route
const APP_ROUTES = [{
  path: 'login',
  CanActivate: [AuthGuardService],
  component: login
}

So when login page loads it's not invoking can activate method and printing console. I have created a guard service and added CanActivate property in routes.

Upvotes: 2

Views: 2119

Answers (1)

ochs.tobi
ochs.tobi

Reputation: 3454

It is because you have an capital C letter in canActivate in your app-routes. Make sure you have registered your AuthGuardService in app.module.ts providers array.

Upvotes: 1

Related Questions