2016-12-15 4 views
0

Ich habe zwei Routen, für die ich mich anpassen möchte, aber sie stehen in Konflikt zueinander. Ich habe eine generische Route, wo path: ":page", aber ich habe eine andere spezifische Route path: "id_token". Idealerweise würde der Router zuerst prüfen, ob path: "id_token" eine Übereinstimmung ist, bevor er dann zu path: ":page" zurückkehrt.Angular 2 Routing - Wie werden Konflikte bei der Routenanpassung behandelt?

export const ROUTES: Route[] = [ 

    // If this route is a match then use this config 
    { 
     path: "id_token", 
     pathMatch: "prefix", 
     redirectTo: ":page/d", 
    }, 

    // Otherwise use this config 
    { 
     path: ":page", 
     component: "PageComponent", 
    } 
]; 

Antwort

1

Sie können CanActive verwenden für dieses

@Injectable() 
class MyCanActivate implements CanActivate { 
    constructor() {} 
    canActivate(
     route: ActivatedRouteSnapshot, 
     state: RouterStateSnapshot 
    ): Observable<boolean>|Promise<boolean>|boolean { 
     return true/false; //to active/deactive your route 
    } 
} 

Dann für Ihre Routen:

export const ROUTES: Route[] = [ 

    // If this route is a match then use this config 
    { 
     path: "id_token", 
     pathMatch: "prefix", 
     redirectTo: ":page/d", 
     canActivate: [MyCanActivate] 
    }, 

    // Otherwise use this config 
    { 
     path: ":page", 
     component: "PageComponent", 
    } 
];