2

Ich verwende Code wie folgt RouterOutlet zu erweitern und App weite Authentifizierung und RoutenschutzLoggedInOutlet angular2-Authentifizierung - Router v3.0.0-alpha8 - Wo ist ComponentInstruction?

import {Directive, Attribute, ViewContainerRef, DynamicComponentLoader} from '@angular/core'; 
import {Router, ComponentInstruction} from '@angular/router'; 
import {Router} from '@angular/router'; 
import {RouterOutletMap} from '@angular/router/src/router_outlet_map'; 
import {RouterOutlet} from '@angular/router/src/directives/router_outlet'; 

import {Authentication} from '../common/authentication.service'; 

@Directive({ 
    selector: 'router-outlet' 
}) 
export class LoggedInRouterOutlet extends RouterOutlet { 
    publicRoutes:any; 
    isAuthenticated:boolean; 
    //private router: any; 

    constructor(public _elementRef: ElementRef, public _loader: DynamicComponentLoader, 
       public _parentRouter: Router, @Attribute('name') nameAttr: string, public authService:Authentication) { 
    super(_elementRef, _loader, _parentRouter, nameAttr); 
    this.isAuthenticated = authService.isLoggedIn(); 


    //this.router = _parentRouter; 
    /** 
    * DEFINE PUBLIC ROUTES 
    * 
    * The Boolean following each route below denotes whether the route requires authentication to view. 
    * 
    * Format: key/value pair 
    * - key is the /route url "/login", "/signup", etc 
    * - value is a boolean true/false 
    * `true` means it's a publicly available route. No authentication required 
    * `false` means it's a protected route which is hidden until user is authenticated 
    * 
    */ 
    this.publicRoutes = { 
     'login': true, 
     'signup': true, 
     '404': true 
    }; 
    } // end constructor 

    routeIsActive(routePath:string) { 
    return this.router.url == routePath; 
    } 

    activate(instruction: ComponentInstruction) { 
    // let url = instruction.urlPath; 
    let url = this.router.url; 
    // If the url doesn't match publicRoutes and they are not authenticated... 
    if (!this.publicRoutes[url] && !this.isAuthenticated) { 
     // todo: redirect to Login, may be there a better way? 
     this.router.navigateByUrl('/login'); 
    } 
    return super.activate(instruction); 
    } 
} 

Problem zu schaffen ist, dass ComponentInstruction nicht in der neuen v3.0.0-alpha8 Router vorhanden ist, und die Super-Methodensignatur hat sich geändert . Wie aktualisiere ich das, um im neuen Router zu arbeiten? Ich kann keine Dokumentation finden, die die Änderungen erklärt.

Antwort

3

ComponentInstruction ist veraltet. In der aktuellen RC4-Version von Angular2 wurde diese Klasse unter reouter-deprecated aufgeführt. Mit RC5 würde dieses Paket fallen gelassen werden.

RouterOutlet hat sich im Laufe der Zeit stark verändert und um Ihre Klasse LoggedInRouterOultet arbeiten zu lassen, müssen Sie die Schnittstelle CanActivate verwenden.

Sie können etwas tun:

import { Injectable } from '@angular/core'; 
import { Router, CanActivate } from '@angular/router'; 
import { LogInService } from './login.service'; 

@Injectable() 
export class LoggedInActivator implements CanActivate { 
    constructor(private loginService: LogInService) {} 

    canActivate() { 
    return this.loginService.isLoggedIn(); 
    } 
} 

canActivate und wo es sich auf die Komponente zu LoggedInActivator hinzufügen, während Route definieren:

Haben ein injizierbares Dienst wie LoggedInActivator hier gezeigten

{ path: 'home', component: HomeComponent, canActivate: [LoggedInActivator] } 

Ich hoffe, das hilft!

+1

Siehe auch https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard –