0
import { ViewContainerRef, ComponentFactoryResolver, Directive, ResolvedReflectiveProvider} from '@angular/core';
import { Router, RouterOutlet, ActivatedRoute, RouterOutletMap } from '@angular/router';

@Directive({
    selector: 'router-outlet'
})
export class ApplicationRouter extends RouterOutlet {
    publicRoutes: Array;
    private parentRouter: Router;
    private router: Router;

    constructor(parentOutletMap: RouterOutletMap, location:ViewContainerRef, componentFactoryResolver: ComponentFactoryResolver, name: string) {
        super(parentOutletMap, location, componentFactoryResolver, name);
        this.router = _parentRouter;
    }

    activate(activatedRoute: ActivatedRoute, providers: ResolvedReflectiveProvider[], outletMap: RouterOutletMap) {
        debugger;
       // return super.activate(instruction);
    }

}

I I don't know the types for the super class to be instantiated, the purpose is to move authorization to router level.

Nicu
  • 3,476
  • 4
  • 23
  • 43

2 Answers2

1

Are you interested on extending RouterOutlet for particular reasons or just to add an authentication layer to your routes? For the latter just change your auth class from "extends RouterOutlet" to "implements CanActivate".

An example of "AuthenticationGuard" implementation:

import { CanActivate }    from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private router: Router) {}

  canActivate() {
    if (this.authService.isLoggedIn) { return true; }
    this.router.navigate(['/login']);
    return false;
  }
}

And then on your routes file(s) add the "CanActivate" property to the routes you want an access control layer:

//route file
...
{
  path: 'admin',
  component: CrisisAdminComponent,
  canActivate: [AuthGuard]
},
...

Simple as that.

From angular docs router guards session

leovrf
  • 605
  • 1
  • 6
  • 17
1

I had the same problem after Angular upgrade to RC4. One of the application components was extending RouterOutlet

RouterOutlet has changed a lot over time and to make your class ApplicationRouter work, you need to make use of router guarding by implementing CanActivate interface.

Please check my answer here and to read more on router guarding refer this.

I hope this helps!

Community
  • 1
  • 1
Satish
  • 2,478
  • 2
  • 17
  • 22