繁体   English   中英

角度CLI生产版本出错

[英]Error at angular CLI production build

我正在尝试使用angular cli版本1.5.2的此命令来构建生产的角度版本5应用程序:

ng build --prod

但是它给了我这个错误:

错误中的错误:静态解析符号值时遇到错误。 调用功能“ FlashMessagesModule”时,不支持函数调用。 考虑将函数或lambda替换为对导出函数的引用,在D:/Project/mean-auth-app/angular-src/src/app/app.module.ts中解析mbol AppModule,在其中解析符号AppModule D:/Project/mean-auth-app/angular-src/src/app/app.mod ule.ts

似乎angular v5.0与angular2-flash-messages模块版本2.0.0有冲突。 我在这里做了完全相同的事情来安装和设置Flash Messages模块。 我进行了搜索,但找不到任何有用的提示。 有人称其为错误,有些人可以通过卸载/安装有问题的软件包来解决问题。

我的应用程序模块:

 import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { RouterModule, Routes } from '@angular/router'; import { HttpModule } from '@angular/http'; import { AppComponent } from './app.component'; import { NavbarComponent } from './components/navbar/navbar.component'; import { LoginComponent } from './components/login/login.component'; import { RegisterComponent } from './components/register/register.component'; import { HomeComponent } from './components/home/home.component'; import { DashboardComponent } from './components/dashboard/dashboard.component'; import { ProfileComponent } from './components/profile/profile.component'; import { AccountService } from './services/account.service'; import { FlashMessagesModule } from 'angular2-flash-messages'; import { JwtModule } from '@auth0/angular-jwt'; import { HttpClientModule } from '@angular/common/http'; import { AuthGuard } from './services/auth-guard.service'; const routes = [ { path: '', component: HomeComponent }, { path: 'register', component: RegisterComponent }, { path: 'login', component: LoginComponent }, { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard]}, { path: 'profile', component: ProfileComponent, canActivate: [AuthGuard]} ]; @NgModule({ declarations: [ AppComponent, NavbarComponent, LoginComponent, RegisterComponent, HomeComponent, DashboardComponent, ProfileComponent ], imports: [ BrowserModule, FormsModule, HttpModule, FlashMessagesModule.forRoot(), RouterModule.forRoot(routes), HttpClientModule, JwtModule.forRoot({ config: { tokenGetter: () => { return localStorage.getItem('token'); }, whitelistedDomains: ['localhost:4200'] } }) ], providers: [AccountService, AuthGuard], bootstrap: [AppComponent] }) export class AppModule { } 

我很高兴能解决这个问题。

编辑:以下内容也会破坏--prod版本,但该消息标志着另一个问题。

我看了一下angular2-flash-messages包,错误在于他们的代码:他们没有添加angular编译器所需的元数据。 在解决以下问题之前,没有解决方案: https : //github.com/moff/angular2-flash-messages/issues/31


旧答案:

不允许在装饰器中使用lambda函数。

错误在这里:

config: {
    tokenGetter: () => {  // <- here
        return localStorage.getItem('token');
    },
    whitelistedDomains: ['localhost:4200']
}

问题是,如果您使用lambda函数,那么angular将无法存储有关装饰器的所有必需信息,但可以使用导出的函数。

您必须将其重写为:

export function tokenGetter() {
    return localStorage.getItem('token');
}

并且您应该能够在代码中像这样使用它:

config: {
    tokenGetter: tokenGetter,
    whitelistedDomains: ['localhost:4200']
}

在AngularCLI的tsconfig.json中指定@angular的路径可以防止发生此错误。

"paths": { "@angular/*": ["../node_modules/@angular/*"] }

参考: 链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM