如何使用angular4
从appsettings.json
到typescript文件读取键值
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"Application": {
"ServiceUrl": "http://localhost:12345",
"BaseURL": ""
},
答案 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;
});
}
}