2016-11-11 4 views
2

Ich bin im Kampf um eine Lösung für dieses Problem zu finden. Ich versuche, Wächter auf eine bestimmte Route zu setzen, die ich in einer externen Routing-Datei eingerichtet habe. Dieser Wächter prüft, ob der Benutzer angemeldet ist, um auf diese Route zuzugreifen, oder der Benutzer wird zum Anmeldebildschirm weitergeleitet.Angular2 Fehler: Unerwarteter Wert 'AuthGuard' exportiert vom Modul 'AppModule'

Das ist mein app.module.ts Datei:

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 { } 

Ich dachte, den Import der Auth-gaurd.service und auth.service und Export beide benötigt würde, als wenn ich sie vom Export ausschließen ich ein Fehler, der besagt, dass AuthGuard exportiert werden muss.

Dies ist meine Routing-Datei.

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}, 
]) 

Schließlich dies mein AuthGuard Dienst ist, die mit dem Auth-Service in Verbindung steht, die isLoggedIn auf true eingestellt hat, wenn der Benutzer angemeldet ist

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; 
     } 
    } 
} 

Diese ist die Stapelfehlermeldung:

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) 

Jede Hilfe wäre grea tally geschätzt. Thanx

Antwort

2

benötigen Sie fügen AuthGuard und AuthService in providers Abschnitt in ngModule, nicht in exports oder dieses Tutorial sucht ROUTING & NAVIGATION aus Winkel Team.

Beispiel von Code aus Tutorial:

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

Ah vielen Dank! Ich glaube nicht, dass ich das verpasst habe. – Rossco

+0

Gern geschehen :) – Dzmitry

Verwandte Themen