我正在使用 Angular/Typescript 编写单页应用程序。该应用程序以登录页面开始。当用户通过身份验证时,使用这行代码加载一个新页面:
this.router.navigate(['/productionFiles']);
这很好用!两个路由器路径也装饰有canactivate [ AuthGuard ]
canactivate
当应用程序将用户路由到生产文件页面时,这很有效。但是在同一页面上刷新会出现以下错误:
Exception: Call to Node module failed with error: Error: Uncaught (in promise): Error: No provider for AuthGuard!
另外,当我手动输入 URL 时也会出现同样的错误?
我知道这个错误在这里还有很多问题。但我认为这有点不同。所以它似乎工作正常,除非有浏览器刷新或手动输入的 URL。
在身份验证服务中,正在设置 localstorage('currentuser'):
localStorage.setItem('currentUser', JSON.stringify(user));
由于我使用 Visual Studio 的 SPA 模板,因此有 3 个 app.module 文件:
App.Module.Shared.Ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './components/app/app.component';
import { HomeComponent } from './components/home/home.component';
import { HeaderComponent } from './components/Header/header.component';
import { ProductionfileComponent } from './components/ProductionFile/productionfile.component';
import { ProductionFileSummary } from './components/ProductionFile/productionfilesummary.component';
import { ProductionFileDetailComponent } from './components/ProductionFile/productionfiledetail.component';
import { LoginComponent } from './components/Login/login.component';
import { AuthGuard } from './components/Login/auth.guard';
export const sharedConfig: NgModule = {
declarations: [
AppComponent,
HomeComponent,
HeaderComponent,
ProductionfileComponent,
ProductionFileSummary,
ProductionFileDetailComponent,
LoginComponent
],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'productionFiles', component: ProductionfileComponent, canActivate: [ AuthGuard ] },
{ path: 'productionfiledetail/:prodHeaderOrdNr', component: ProductionFileDetailComponent, canActivate: [ AuthGuard ] },
{ path: '**', redirectTo: 'login' }
]),
BrowserModule,
FormsModule
],
providers: [AuthGuard],
bootstrap: [AppComponent]
};
App.Module.Client.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';
@NgModule({
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpModule,
...sharedConfig.imports
],
providers: [ sharedConfig.providers,
{ provide: 'ORIGIN_URL', useValue: location.origin }
],
bootstrap: [sharedConfig.bootstrap]
})
export class AppModule {
}
App.Module.Server.ts:
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { sharedConfig } from './app.module.shared';
@NgModule({
declarations: sharedConfig.declarations,
imports: [
ServerModule,
...sharedConfig.imports
],
providers: sharedConfig.providers,
bootstrap: sharedConfig.bootstrap
})
export class AppModule {
}
有人可以让我走上正轨吗?