2016-11-11 83 views
2

我正在努力尋找針對此問題的解決方案。我正試圖將警衛放在我在外部路由文件中設置的特定路線上。此警衛將檢查用戶是否登錄以訪問此路線或將用戶重定向到登錄屏幕。Angular2錯誤:由模塊'AppModule'導出的意外值'AuthGuard'

這是我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服務,與具有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) 

任何幫助將grea tly讚賞。感謝名單

回答

2

您需要添加AuthGuardAuthServiceproviders節ngModule,而不是在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 {} 
+0

丫太感謝你了!不能相信我錯過了那個 – Rossco

+0

不客氣:) – Dzmitry

相關問題