2017-02-20 55 views
0

我试图使用他们的教程添加Auth0的授权: https://auth0.com/docs/quickstart/spa/angular2/07-authorization如何将条件添加到路由

我使用NG2管理员,但他们有这样一个不同的结构通常。我发现pages.routing.ts,并添加了第三个参数。我还从教程中提供了其他auth.guard服务。但是每当我去到我使用canActivate的页面。它只是加载背景并说:EXCEPTION: Uncaught (in promise): Error: DI Error

我做了很多调试,我99%确定它是导致问题的canActivate。非其他类别甚至被称为。

在上auth0网站上的例子也提到:

export const appRoutingProviders: any[] = [ 
    AuthGuard 
]; 

这我尝试添加,但这并没有改变任何东西。

我想路由调用canActivate,以便我可以Auth0的身份验证。

import { Routes, RouterModule } from '@angular/router'; 
import { Pages } from './pages.component'; 
import { ModuleWithProviders } from '@angular/core'; 
import { AuthGuard } from '../auth.guard'; 
export const routes: Routes = [ 
    { 
    path: 'login', 
    loadChildren: 'app/pages/login/login.module#LoginModule' 
    }, 
    { 
    path: 'register', 
    loadChildren: 'app/pages/register/register.module#RegisterModule' 
    }, 
    { 
    path: 'pages', 
    component: Pages, 
    children: [ 
     { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, 
     { path: 'dashboard', loadChildren: 'app/pages/dashboard/dashboard.module#DashboardModule',canActivate: [AuthGuard] }, 
     { path: 'editors', loadChildren: 'app/pages/editors/editors.module#EditorsModule',canActivate: [AuthGuard] }, 
     { path: 'components', loadChildren: 'app/pages/components/components.module#ComponentsModule',canActivate: [AuthGuard] }, 
     { path: 'charts', loadChildren: 'app/pages/charts/charts.module#ChartsModule',canActivate: [AuthGuard] }, 
     { path: 'ui', loadChildren: 'app/pages/ui/ui.module#UiModule',canActivate: [AuthGuard] }, 
     { path: 'forms', loadChildren: 'app/pages/forms/forms.module#FormsModule',canActivate: [AuthGuard] }, 
     { path: 'tables', loadChildren: 'app/pages/tables/tables.module#TablesModule',canActivate: [AuthGuard] }, 
     { path: 'maps', loadChildren: 'app/pages/maps/maps.module#MapsModule',canActivate: [AuthGuard] }, 
     { path: 'new', loadChildren: 'app/pages/new/new.module',canActivate: [AuthGuard] } 
    ] 
    } 
]; 
+0

是AuthGuard包含在您的提供者中的模块定义中吗? –

回答

2

在你的路由文件试试这个:

@NgModule({ 
    imports: [ 
    RouterModule.forRoot(routes), 
    ], 
providers: [ 
    AuthGuard //add AuthGuard to provider 
    ] 
}) 

您还可以添加AuthGuard到的AppModule提供商,这样你就不需要提供它在每一个路由文件。

+1

https://angular.io/docs/ts/latest/guide/router.html#!#guards –

相关问题