2016-06-20 96 views
3

我想從配置文件中加載所有url。我創建了一個名爲JSON文件「config.json」,ConfigurationService.ts度日關鍵的網址,但我不能鍵獲取URL從Angular 2中的config.json獲取數據

config.json:。

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

ConfigurationService.ts :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?

回答

5

我想你要找的東西是這樣的:

getConfiguration(key) { 
    return this.http.get('./app/config/config.json').map(res => { 
     this.result = res.json(); 
     return this.result[key]; 
    }); 
} 
+1

啊哈。我看到我的錯誤。謝謝:) – Serhiy