从配置文件读取Angular2

从配置文件读取Angular2,angular,Angular,我正在制作一个新的angular2应用程序,它将命中我们所有服务器上所有应用程序的一个端点,以获取内部版本号,然后将它们显示在网格中,以便我们可以看到哪些服务器具有哪些内部版本号 当我使用Angular1构建一个类似的应用程序时,我能够使用读取JSON文件并输出一个带有JSON值的angular模块。我希望在Angular2中做类似的事情,但我不知道如何实现这一点。有什么想法吗 Normal$http对配置文件的get调用应该满足您的要求 $http.get('path-to-configura

我正在制作一个新的angular2应用程序,它将命中我们所有服务器上所有应用程序的一个端点,以获取内部版本号,然后将它们显示在网格中,以便我们可以看到哪些服务器具有哪些内部版本号


当我使用Angular1构建一个类似的应用程序时,我能够使用读取JSON文件并输出一个带有JSON值的angular模块。我希望在Angular2中做类似的事情,但我不知道如何实现这一点。有什么想法吗

Normal$http对配置文件的get调用应该满足您的要求

$http.get('path-to-configuration-file')
   .success(function (confData) {
     // Use your conf data however you like
   })
   .error(function () {
      // File not found
   });

我希望这可以正常工作

Normal$http对配置文件的get调用应该满足您的要求

$http.get('path-to-configuration-file')
   .success(function (confData) {
     // Use your conf data however you like
   })
   .error(function () {
      // File not found
   });

我希望这能起作用

这样你就可以阅读
Angular2
中的
.json
文件了
注意:通过这种方式,您可以读取
.json
文件。此演示显示了
.json
文件示例

=>单击朋友选项卡

http.get('friends.json (=filepath)')
    .map(res => res.json())
    .subscribe((data) => {  this.result=data; },
                            err=>console.log(err),
                            ()=>console.log('done')
               );

这样您就可以在
Angular2
中读取
.json
文件
注意:通过这种方式,您可以读取
.json
文件。此演示显示了
.json
文件示例

=>单击朋友选项卡

http.get('friends.json (=filepath)')
    .map(res => res.json())
    .subscribe((data) => {  this.result=data; },
                            err=>console.log(err),
                            ()=>console.log('done')
               );

在Angular2版本2.1.0.0中读取配置文件的最佳方法是,您的服务如下所示

    @Injectable()
        export class ConfigService {
          private Server:string = "";
          private AppKey = "";
          constructor(private _http:Http) {
          }
          public load() {
            return new Promise((resolve, reject) => {
              this._http.get('config.json')  // path of your config.json file
                .map(res => res.json())
                .subscribe(
                  (data:any) => {
                    this.Server = data.server;
                    this.AppKey = data.appkey;
                    resolve(true);
                  },
                  err=>console.log(err)
                );
            });
          }

      public getServer(){
           return  this.Server;
        }

    public getAppKey(){
           return  this.AppKey;
        }
  }
您必须像这样加载此配置app.module.ts

@NgModule({
  declarations: [
  ..
  ],
  imports: [
    ...
    ],
  providers: [ConfigService,
      {
      provide: APP_INITIALIZER,
      useFactory: (config:ConfigService) => () => config.load(),
      deps: [ConfigService],
      multi: true
    }


  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

在Angular2版本2.1.0.0中读取配置文件的最佳方法是,您的服务如下所示

    @Injectable()
        export class ConfigService {
          private Server:string = "";
          private AppKey = "";
          constructor(private _http:Http) {
          }
          public load() {
            return new Promise((resolve, reject) => {
              this._http.get('config.json')  // path of your config.json file
                .map(res => res.json())
                .subscribe(
                  (data:any) => {
                    this.Server = data.server;
                    this.AppKey = data.appkey;
                    resolve(true);
                  },
                  err=>console.log(err)
                );
            });
          }

      public getServer(){
           return  this.Server;
        }

    public getAppKey(){
           return  this.AppKey;
        }
  }
您必须像这样加载此配置app.module.ts

@NgModule({
  declarations: [
  ..
  ],
  imports: [
    ...
    ],
  providers: [ConfigService,
      {
      provide: APP_INITIALIZER,
      useFactory: (config:ConfigService) => () => config.load(),
      deps: [ConfigService],
      multi: true
    }


  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

虽然这实际上是有道理的,但你的意思不就是Http
Http
?我认为$http不在其中angular2@AlexKibler:谢谢你的更正。Http就是我的意思。对angular2不太熟悉,但作为一个想法,http应该可以解决这个问题。虽然这实际上是有道理的,但你不是说
http
?我认为$http不在其中angular2@AlexKibler:谢谢你的更正。Http就是我的意思。虽然不太熟悉angular2,但作为一个想法,http应该可以解决这个问题,感谢micronyks!一如既往地感谢麦克隆!使用json文件来存储诸如secretkey和clientId之类的合理配置数据可以吗?config.json不是一个公共文件吗?@JuanRojas我会说不是。在我的例子中,它只存储像主机和端口这样的服务器信息。使用json文件来存储像secretkey和clientId这样的合理配置数据可以吗?config.json不是一个公共文件吗?@JuanRojas我会说不是。在我的例子中,它只存储像主机和端口这样的服务器信息