Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular 2应用程序启动前读取配置文件?_Angular - Fatal编程技术网

如何在Angular 2应用程序启动前读取配置文件?

如何在Angular 2应用程序启动前读取配置文件?,angular,Angular,在Angular 2框架呈现任何组件或服务之前,我需要json文件中的一些配置数据。我还没有找到任何有效的解决办法来实现这一目标 我使用Angular 2 RC6与Angular 1在混合模式下工作,因此我使用升级适配器执行引导 你有什么想法来实现我需要的吗?在“main.ts”文件中,我使用jQuery“$.getJSON”函数加载配置文件,在回调时,我调用引导函数: declare var $: any; declare var configFile: string; import './

在Angular 2框架呈现任何组件或服务之前,我需要json文件中的一些配置数据。我还没有找到任何有效的解决办法来实现这一目标

我使用Angular 2 RC6与Angular 1在混合模式下工作,因此我使用升级适配器执行引导

你有什么想法来实现我需要的吗?

在“main.ts”文件中,我使用jQuery“$.getJSON”函数加载配置文件,在回调时,我调用引导函数:

declare var $: any;
declare var configFile: string;

import './polyfills.ts';

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { environment } from './environments/environment';
import { AppModule } from './app';

if (environment.production) {
    enableProdMode();
    this.configFile = 'assets/config.prod.json';
} else {
    this.configFile = 'assets/config.json';
}

$.getJSON(this.configFile, (data) => {
    environment.config = data;

    platformBrowserDynamic().bootstrapModule(AppModule);
});
通过这种方式,您可以确保在配置数据完全加载后执行引导。

查看此博客:

它使用
APP\u初始值设定项
在应用程序启动前钩住
AppConfigService
,并获取数据。有关
AppConfigService
的实现,请参见博客

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { AppConfigService } from './services/app-config.service';

export function appInit(appConfigService: AppConfigService) {
  return () => appConfigService.load();
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
  ],
  providers: [AppConfigService,
    {
      provide: APP_INITIALIZER,
      useFactory: appInit,
      multi: true,
      deps: [AppConfigService]
    }],
  bootstrap: [AppComponent]
})
export class AppModule { }

对不起,我不确定你的链接怎么样?糟糕的主意。。首先,根本不需要jquery,更好地使用服务