簡體   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