【问题标题】:ERROR in Error encountered resolving symbol values statically - Can't build Angular 2 app错误中的错误遇到静态解析符号值 - 无法构建 Angular 2 应用程序
【发布时间】:2017-08-02 23:06:33
【问题描述】:

当我运行ng serve 命令时,我收到了这个错误:

错误中的错误遇到静态解析符号值。 不支持函数调用。考虑更换功能或 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 Code 中编辑代码并在嵌入式控制台中运行 ng serve 命令。有什么想法吗?

【问题讨论】:

    标签: angular angular-cli


    【解决方案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 { }
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2017-02-13
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2018-01-03
      相关资源
      最近更新 更多