错误中的错误遇到静态解析符号值 - 无法构建Angular 2应用程序

时间:2017-03-13 00:50:36

标签: angular angular-cli

当我运行ng serve命令时,我收到此错误:

  

ERROR in Error遇到静态解析符号值。   不支持函数调用。考虑更换功能或   lambda引用了一个导出函数(位置42:45)   原始.ts文件),解析符号AppModule   的 C:/myapp/src/app/app.module.ts

这是我的 app.module.ts

...

import { APP_INITIALIZER } from '@angular/core';
import { AppConfig } from './app.config';

...

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'listings', component: ListingComponent },
  { path: 'add-listing', component: AddListingComponent },
];

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(appRoutes)
  ],
  providers: [
    AppConfig,
    { provide: APP_INITIALIZER, useFactory: (config: AppConfig) => () => config.load(), deps: [AppConfig], multi: true }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

它抱怨的是:

  

{提供:APP_INITIALIZER,useFactory:(config:AppConfig)=> ()=>   config.load(),deps:[AppConfig],multi:true}

这是我的 app.config.ts (AppConfig)类:

import { Inject, Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class AppConfig {

    private config: Object = null;

    constructor(private http: Http) { }

    public get(key: any) {
        return this.config[key];
    }

    public load() {
        return new Promise((resolve, reject) => {
            this.http.get('config.json').map( res => res.json() ).catch((error: any):any => {
                console.log('Configuration file "config.json" could not be read');
                resolve(true);
                return Observable.throw(error.json().error || 'Internal Server Srror');
            }).subscribe( (responseData: Object) => {
                this.config = responseData;
                resolve(true);
            });
        });
    }
}

以下是我的工具版本:

angular-cli: 1.0.0-beta.28.3
node: 7.7.2
os: win32 x64
@angular/common: 2.4.9
@angular/compiler: 2.4.9
@angular/core: 2.4.9
@angular/forms: 2.4.9
@angular/http: 2.4.9
@angular/platform-browser: 2.4.9
@angular/platform-browser-dynamic: 2.4.9
@angular/router: 3.4.9
@angular/compiler-cli: 2.4.9

我正在编辑 Visual Studio代码中的代码并在嵌入式控制台中运行ng serve命令。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

感谢这个答案; https://stackoverflow.com/a/41263119/2332336

我已经修好了这个:

export function appConfigFactory(config: AppConfig) {
  return () => config.load();
}

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [
    AppConfig,
    { provide: APP_INITIALIZER, useFactory: appConfigFactory, deps: [AppConfig], multi: true }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }