如何使用angular4从appsettings.json读取键值文件

时间:2018-05-03 13:48:29

标签: json angular appsettings

如何使用angular4

appsettings.json到typescript文件读取键值
{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "Application": {
    "ServiceUrl": "http://localhost:12345",
    "BaseURL": ""
  },

1 个答案:

答案 0 :(得分:1)

根据此JSON文件在Web应用程序中的位置,您可以使用Angular的Http客户端执行GET请求并获取其数据。您甚至可以添加一个打字稿界面来获得结果。有点像这样:

import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';

export interface someInterface {
   foo: string,
   bar: number
}

export class SomeClass {
  constructor(private http: HttpClient) { }
  data: someInterface;

  someFunc(){
    this.http.get<someInterface>('/path/to/json').subscribe(result => {
       this.data = result;
    });
  }
}