【发布时间】:2019-03-30 11:34:15
【问题描述】:
我的应用无法在生产环境中运行。
我的应用使用来自@auth0/angular-jwt 的angular-jwt
如果用户有一个有效的令牌,他将被转发到 MainScreenComponent,在其他地方他将被重定向到登录页面。
import { AuthGuard } from './services/Authentication/auth-guard.service';
import { AuthGuardLogin } from './services/Authentication/auth-guard-login.service';
import { AuthService } from './services/Authentication/auth.service';
import { BrowserModule , HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { LoginComponent } from './components/login/login.component';
import { NotFoundComponent } from './components/not-found/not-found.component';
import { NoAccessComponent } from './components/no-access/no-access.component';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { ServiceWorkerModule } from '@angular/service-worker';
import { MainScreenComponent } from './components/main-screen/main-screen.component';
import { JwtModule } from '@auth0/angular-jwt';
export function tokenGetter() {
return localStorage.getItem('token');
}
@NgModule({
declarations: [
AppComponent,
LoginComponent,
NotFoundComponent,
NoAccessComponent,
MainScreenComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
FormsModule,
ReactiveFormsModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
RouterModule.forRoot([
{ path: '', component: MainScreenComponent , canActivate: [AuthGuard]},
{ path: 'login', component: LoginComponent, canActivate: [AuthGuardLogin] },
{ path: '**', component: NotFoundComponent }
]),
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
whitelistedDomains: environment.whiteListDomains,
}
})
],
providers: [
AuthService,
AuthGuard,
AuthGuardLogin,
],
bootstrap: [AppComponent]
})
export class AppModule { }
如果我使用 ng serve 一切正常, 但是在 ng build --prod 之后,如果我的初始 URL 是 https://my-site:4300/login,我会得到 404。 在这种情况下,RouterModule 设置将被忽略。
如果我输入 https://my-site:4300,我将被重定向到 https://my-site:4300/login 并显示登录页面。
有什么想法吗?
==========================
2018 年 10 月 31 日更新:
这里是 AuthGuardLogin:
import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuardLogin implements CanActivate {
constructor( private router: Router, private authService: AuthService) { }
canActivate() {
if (this.authService.isLoggedIn() === false) {
return true; }
this.router.navigate(['/']);
return false;
}
}
我现在所做的是将网站托管在 Kestrel 服务器中。 现在它完美无缺。为什么?我不知道:-)!
顺便说一下,Kestrel 服务器托管在 Windows 服务中。所以我可以使用我们的软件包进行部署,并通过常规设置进行安装。
最好的问候,非常感谢你!!!
【问题讨论】:
-
你在 index.html 页面上使用
href="/"吗? -
是的。
-
environment.prod.ts是否配置正确?你能比较一下environment.prod.ts和environment.dev.ts或environment.ts吗?也许环境文件上的一些变量正在造成问题 -
嗨。除了生产属性之外,两者完全相同。
-
让我们看看 git github.com/angular/angular-cli/issues/8515 上的这个 Angular cli 问题,也许你正在用 ServiceWorker 和 base-href 进行类似的实验