Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Angular 2中的config.json获取数据_Angular - Fatal编程技术网

从Angular 2中的config.json获取数据

从Angular 2中的config.json获取数据,angular,Angular,我想从配置文件加载所有url。我已经创建了名为'config.json',ConfigurationService.ts的json文件来按键获取URL。但是我无法按键获取URL config.json: { "loginUrl": "http:example/login", "verifyUrl": "http:example/verify" } ConfigurationService.ts: import {Injectable} from '@angular/core'; import

我想从配置文件加载所有url。我已经创建了名为'config.json',ConfigurationService.ts的json文件来按键获取URL。但是我无法按键获取URL

config.json:

{
"loginUrl": "http:example/login",
"verifyUrl": "http:example/verify"
}
ConfigurationService.ts:

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class ConfigurationService {
constructor(private http:Http) {}

private result: Object;

getConfiguration(key) {
    return this.http.get('./app/config/config.json').map(res => {
        res.json();
        this.result = res._body;
        return this.result[key]; //returns 'undefined', but when I return only 'this.result'  it shows me all json data {...}
    });
}
}
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));
auth_service.ts的一部分:

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class ConfigurationService {
constructor(private http:Http) {}

private result: Object;

getConfiguration(key) {
    return this.http.get('./app/config/config.json').map(res => {
        res.json();
        this.result = res._body;
        return this.result[key]; //returns 'undefined', but when I return only 'this.result'  it shows me all json data {...}
    });
}
}
private x =this.configurationService.getConfiguration("verifyUrl").subscribe((result) => console.log(result));

例如,我如何只接收loginUrl?

我想您需要的是:

getConfiguration(键){
返回这个.http.get('./app/config/config.json').map(res=>{
this.result=res.json();
返回此。结果[键];
});
}