在主ts角度读取配置文件

时间:2017-10-09 11:06:11

标签: angular bootstrapping

我见过一些类似的问题,但我无法弄清楚如何做到这一点。我有一个config.json文件,我想在应用程序启动之前读取此文件。我已经创建了一个配置服务,但我无法弄清楚如何在main.ts中调用它(不同于类的结构)

ConfigurationService类

import { Http } from '@angular/http'
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { EmovieCat } from '../model/eMovieCategory';
import { Headers, RequestOptions } from '@angular/http';
import { ConnectionHelper } from "app/config/connections";


@Injectable()
export class ConfigurationService {

    getConfiguration() {
        return this._http.get("app/config/config.json").map(res => res.json());
    }


    constructor(private _http: Http) {

    }
}

Main.ts

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { ConfigurationService } from 'app/service/configurationService';

if (environment.production) {
  enableProdMode();
}

/*I want to do something like below,7
 but i couldnt figure how to instantiate ConfigurationService

ConfigurationService cs = new ConfigurationService();
cs.getConfiguration().subscribe(resp=>{
  //set the server config here.
  platformBrowserDynamic().bootstrapModule(AppModule);
})
*/

1 个答案:

答案 0 :(得分:0)

如果需要,请删除环境参考,并使用Javascript fetch API在main.ts之前加载JSON

django-admin startproject sqlite3test
cd sqlite3test/
python manage.py migrate
    Operations to perform:
        Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
        Applying contenttypes.0001_initial... OK
        ... and so on
ls -al | grep db
    -rw-r--r--.  1 shmakovpn shmakovpn 40960 апр 19 01:02 db.sqlite3

然后,您可以在app.module.ts提供程序中使用APP_INITIALIZER并调用您的服务方法以在模块加载之前加载配置数据。