【问题标题】:Error at angular CLI production build角度 CLI 生产构建时出错
【发布时间】:2017-11-27 13:19:27
【问题描述】:

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

ng build --prod

但它给了我这个错误:

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

似乎 angular v5.0 与 angular2-flash-messages 模块版本 2.0.0 有冲突。 我做了完全相同的事情here 来安装和设置闪存消息模块。我搜索了但我找不到任何有用的提示。有些人称之为错误,有些人可以通过卸载/安装有问题的包来解决他们的问题。

我的应用模块:

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

感谢任何解决此问题的提示。

【问题讨论】:

  • 如消息所述:您不允许在装饰器中使用 lamda 函数。尝试将其重写为您通过tokenGetter 引用的导出函数。
  • @Dinistro 感谢您的回复。但它提到了 FlashMessagesModule
  • FlashMessagesModule 不是导致此错误的原因。

标签: javascript angular angular-cli flash-message


【解决方案1】:

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

我查看了 angular2-flash-messages 包,错误在于它们的代码: 他们不添加角度编译器所需的元数据。 在解决以下问题之前没有解决方案: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']
}

【讨论】:

  • 我用你的建议替换了那部分。仍然给我同样的错误
  • @peymangilmour 消息是否完全相同?还在提AppModule吗?
  • 错误中的错误:静态解析符号值时遇到错误。调用函数“FlashMessagesModule”,不支持函数调用。考虑将函数或 lambda 替换为对导出函数的引用,解析 D:/Project/mean-auth-app/angular-src/src/app/app.module.ts 中的符号 AppModule,解析 D:/ 中的符号 AppModule项目/mean-auth-app/angular-src/src/app/app.module.ts
  • 非常感谢。所以我认为我应该找到一种替代方法,或者等待他们是否可以让它发挥作用。
【解决方案2】:

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

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

参考:link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2019-11-26
    • 2020-01-24
    • 2019-06-03
    • 2020-04-11
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    相关资源
    最近更新 更多