Angular2错误:意外的值' AuthGuard'由模块' AppModule'导出

时间:2016-11-11 10:18:34

标签: angular angular2-routing

我正在努力为我面临的这个问题寻找解决方案。我试图将警卫放在我在外部路由文件中设置的特定路由上。此保护是检查用户是否已登录以访问此路由或用户被重定向到登录屏幕。

这是我的app.module.ts文件:

 import { NgModule } from '@angular/core'; 
import { RouterModule } from '@angular/router'; 
import { UniversalModule } from 'angular2-universal'; 
import { AppComponent } from './app.component'; 
import { Routing } from './app.routing';


import { LoginComponent } from './shared/login/login.component'; 
import { DashboardComponent } from './dashboard/dashboard.component';


import { AuthGuard } from './shared/login/auth-guard.service'; 
import { AuthService } from './shared/login/auth.service';

    @NgModule({
        bootstrap: [ AppComponent ],
        declarations: [
            AppComponent,
            LoginComponent,
            DashboardComponent
        ],
        imports: [
            UniversalModule,
            Routing
        ],
        exports: [
            AuthGuard,
            AuthService
        ] }) export class AppModule { }

我认为导入auth-gaurd.service和auth.service并且需要导出两者,就像我从导出中排除它一样我得到一个错误,说需要导出AuthGuard。

这是我的路由文件:

import { Router, RouterModule } from '@angular/router';

import { AuthGuard } from './shared/login/auth-guard.service';

import { DashboardComponent } from './dashboard/dashboard.component';
import { LoginComponent } from './shared/login/login.component';


export const Routing = RouterModule.forRoot([ 
    { path: '', component: DashboardComponent, canActivate: [AuthGuard] }, 
    { path: 'login', component: LoginComponent},
])

最后,这是我的AuthGuard服务,它与auth服务进行通信,如果用户已登录,则isLoggedIn设置为true。

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';

import { AuthService } from './auth.service';


@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private _authService: AuthService, private _router: Router) {

    }

    canActivate() { // Route gaurd
        if (this._authService.isLoggedIn) { 
            return true;
        } else {
            this._router.navigate(['login']); 
            return false;
        }
    }
}

这是堆栈错误消息:

Exception: Call to Node module failed with error: Error: Unexpected value 'AuthGuard' exported by the module 'AppModule'
at G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\@angular\compiler\bundles\compiler.umd.js:14153:33
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\@angular\compiler\bundles\compiler.umd.js:14136:46)
at RuntimeCompiler._compileComponents (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\@angular\compiler\bundles\compiler.umd.js:16803:49)
at RuntimeCompiler._compileModuleAndComponents (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\@angular\compiler\bundles\compiler.umd.js:16741:39)
at RuntimeCompiler.compileModuleAsync (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\@angular\compiler\bundles\compiler.umd.js:16732:23)
at NodePlatform.bootstrapModule (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\angular2-platform-node\node-platform.js:436:25)
at NodePlatform.serializeModule (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\angular2-platform-node\node-platform.js:108:22)
at G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\ClientApp\dist\main-server.js:74:63
at ZoneDelegate.invoke (G:\HaldanMT-WebApp\HaldanMT-WebApp\HaldanMT-WebApp\node_modules\zone.js\dist\zone-node.js:232:26)

非常感谢任何帮助。感谢名单

1 个答案:

答案 0 :(得分:2)

您需要在ngModule中的AuthGuard部分添加AuthServiceproviders,而不是exports,或者从角色团队查看ROUTING & NAVIGATION的本教程。

教程中的代码示例:

import { AuthGuard }                from '../auth-guard.service';

@NgModule({
  imports: [
    RouterModule.forChild([
      {
        path: 'admin',
        component: AdminComponent,
        canActivate: [AuthGuard],
        children: [
          {
            path: '',
            children: [
              { path: 'crises', component: ManageCrisesComponent },
              { path: 'heroes', component: ManageHeroesComponent },
              { path: '', component: AdminDashboardComponent }
            ],
          }
        ]
      }
    ])
  ],
  exports: [
    RouterModule
  ]
})
export class AdminRoutingModule {}