Angularjs2 - 应用程序启动前的预加载服务器配置

Ras*_*xit 14 angular

我正在使用Angular2 RC5.我希望能够在我的应用程序启动之前加载我的服务器IP地址和一些其他配置参数.我怎样才能在最新的RC5中做到这一点?

我看过其他文章,但它们没有帮助:

  1. 如何在angular2中预加载配置文件:这里提供的答案特定于webpack.我正在使用systemjs.
  2. https://medium.com/@hasan.hameed/reading-configuration-files-in-angular-2-9d18b7a6aa4#.m8lontnas:这是针对Angular2的旧版本.使用的某些类已弃用.

任何帮助将不胜感激.

编辑

我尝试在app.module.ts中使用APP_INITIALIZER,如下所示:

    import { NgModule, provide, APP_INITIALIZER }       from "@angular/core";
    import { ConfigService } from "./shared/services/config.service";
    @NgModule({
        declarations: [AppComponent],
        imports: [BrowserModule,
            routes,
            FormsModule,
            HttpModule],
        providers: [AuthService,
            Title,
            appRoutingProviders,
            ConfigService],
        bootstrap: [AppComponent
            , provide(APP_INITIALIZER,
                {
                    useFactory: (config: ConfigService) => () => config.load(),
                    deps: [ConfigService], multi: true
                })]
    })
    export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

这是我的config.service.ts文件:

    import { Config } from "../model/config";

    export class ConfigService {
        constructor() {
        }

        load(): Promise<Config> {
            let config: Config = new Config("localhost", "5050");
            return Promise.resolve(config);
        }
    }
Run Code Online (Sandbox Code Playgroud)

请注意,我最终将重新写入load以实际读取属性或某些此类文件以加载配置.

这就是config.ts的样子:

export class Config {

    constructor(
        private machine: string,
        private port: string
    ) {
    }

    private base_url: string = "http://" + this.machine +
    this.port + "/";

    public loginURL: string = this.base_url + "login";
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误

public/app/app.module.ts(27,11): error TS2345: Argument of type '{ declarations: (typeof AppComponent ...' is not assignable to parameter of type 'NgModuleMetadataType'.
  Types of property 'bootstrap' are incompatible.
    Type '(typeof AppComponent | Provider)[]' is not assignable to type '(Type | any[])[]'.
      Type 'typeof AppComponent | Provider' is not assignable to type 'Type | any[]'.
        Type 'Provider' is not assignable to type 'Type | any[]'.
          Type 'Provider' is not assignable to type 'any[]'.
            Property '[Symbol.iterator]' is missing in type 'Provider'.
Run Code Online (Sandbox Code Playgroud)

编辑2

我继续下一步.更新ConfigService以使用http.get读取json文件.现在我收到一个错误.以下是更新的文件:

export class ConfigService {
    private config: Config;
        constructor(private http: Http) {
    }

    load(): Promise<Config> {
            this.http.get("/blah/config.json")
                 .map(res => res.json())
                 .subscribe((env_data) => {
                     console.log(env_data);
                  });
        this.config = new Config("localhost", "5050");
        return Promise.resolve(this.config);
    }

    getConfig(): Config {
        return this.config;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是NgModule类中的提供程序部分

    providers: [AuthService,
    Title,
    appRoutingProviders,
    ConfigService,
    {
        provide: APP_INITIALIZER,
        useFactory: (config: ConfigService) => () => config.load(),
        deps: [ConfigService, Http],
        multi: true
    }],
Run Code Online (Sandbox Code Playgroud)

请注意,在RC6中,我无法导入HTTP_PROVIDERS,因为我得到的错误是这些错误不再在http模块中定义.这就是我尝试Http的原因.

在运行时,我得到以下错误

(index):27 Error: Error: Can't resolve all parameters for ConfigService: (?).(…)(anonymous function) @ (index):27
ZoneDelegate.invoke @ zone.js:332
Zone.run @ zone.js:225(anonymous function) @ zone.js:591
ZoneDelegate.invokeTask @ zone.js:365
Zone.runTask @ zone.js:265
drainMicroTaskQueue @ zone.js:497
ZoneTask.invoke @ zone.js:437
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 27

在根模块中提供此功能

{provide: APP_INITIALIZER, useValue: () => promise, multi: true}]}
Run Code Online (Sandbox Code Playgroud)

这里() => promise是一些调用返回解析到期望值的承诺.

另请参见如何将从后端呈现的参数传递给angular2 bootstrap方法

您可以将服务作为依赖项传递,您可以在其中存储结果.

更新

export function loadConfig(config: ConfigService) => () => config.load()

@NgModule({
        declarations: [AppComponent],
        imports: [BrowserModule,
            routes,
            FormsModule,
            HttpModule],
        providers: [AuthService,
            Title,
            appRoutingProviders,
            ConfigService,
            { provide: APP_INITIALIZER,
              useFactory: loadConfig,
              deps: [ConfigService], 
              multi: true }
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

更新2

Plunker的例子

  • 使用AOT时,useFactory不应该获得动态函数,而应该是导出函数.请参见http://stackoverflow.com/a/41636880/499839 (4认同)