简体   繁体   中英

How to read key values from appsettings.json to typescript file using angular4

How to read key values from appsettings.json to typescript file using angular4

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "Application": {
    "ServiceUrl": "http://localhost:12345",
    "BaseURL": ""
  },

Depending on where this JSON file is located in your Web application, you would be able to use Angular's Http client to do a GET request and fetch it's data. You can even add a typescript interface to expect a result. A bit like this:

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;
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM