[英]Angular 2+ share json config values with all http services
I have an angular service which reads a json file containing a rest fqdn and rest port ({ "restFqdn":"kuno.dev.net", "restPort":"58085" }). 我有一个角度服务,它读取一个包含rest fqdn和rest端口的json文件({“ restFqdn”:“ kuno.dev.net”,“ restPort”:“ 58085”})。 I want share the fqdn and port with all other http services, which get json data from a backend rest server.
我想与所有其他http服务共享fqdn和port,这些服务从后端Rest服务器获取json数据。 The fqdn and port are static after being read from the json file, but the json file can contain different values for a particular deployment.
从json文件读取后,fqdn和port是静态的,但是json文件可以包含用于特定部署的不同值。
@Injectable()
export class FileService {
private result: Object;
private restFqdn = '';
private restPort = '';
constructor(private http:Http) {
this.result = this.getConfFile();
console.log('FileService - constructor: ', this.result)
this.setFqdn(this.result[0]);
this.setPort(this.result[1]);
}
getConfFile() {
return this.http.get('json/conf.json')
.map( (res:Response) => {
this.result = res.json();
console.log('FileService - getConfFile(): ', this.result)
return this.result;
}
)
.catch( error =>
Observable.throw(error || 'Server error')
);
};
setFqdn(value) {
this.restFqdn = value;
console.log('FileService - setFqdn(): ', this.restFqdn);
}
getFqdn() {
return this.restFqdn;
}
I am not sure how to share the fqdn and port with my services. 我不确定如何与我的服务共享fqdn和端口。 I don't really think this is state information, but do I try and use an external library like ngrx/store to do this or is there some other approach that works?
我真的不认为这是状态信息,但是我是否尝试使用外部库(例如ngrx / store)来执行此操作,或者是否有其他可行的方法? This is what I currently have in my RateService.ts:
这是我当前在RateService.ts中拥有的内容:
constructor(private _http: HttpClient, private _fileService: FileService) {
this._fileService.getConfFile().subscribe (
data => {
this.restFqdn = data.restFqdn;
this.restPort = data.restPort;
this.rateApi = '/kuno/rate';
this.rateUrl = 'http://' + this.restFqdn + ':' + this.restPort + this.rateApi;
console.log('RateService - constructor() - restFqdn: ', this.restFqdn);
console.log('RateService - constructor() - restPort: ', this.restPort);
},
err => {
this.errorMessage = 'Error - getConfFile failed!';
}
);
}
The constructor prints out the correct values from the json file for rest fqdn and rest port. 构造函数从json文件中为rest fqdn和rest端口输出正确的值。 However, when I execute my getRates call, the fqdn and port are undefined.
但是,当我执行getRates调用时,未定义fqdn和port。 What do I need to do to get the values propagated to getRates()?
我需要怎么做才能将值传播到getRates()?
getRates(): Observable<any> {
console.log('RateService - getRates() - getFqdn(): ', this._fileService.getFqdn()); // prints undefined
return this._http.get(this.rateUrl)
.do(data => {
console.log('RateService: getRates(): ' + JSON.stringify(data))
})
.catch(this.handleError);
}
Many thanks! 非常感谢!
I suspect the following: 我怀疑以下情况:
this.setFqdn(this.result[0]);
this.setPort(this.result[1]);
What it does is gets the 0th and 1st element from the array of response and sets it, but seeing at you config.json file, the request is in this format: 它的作用是从响应数组中获取第0个和第1个元素并进行设置,但是在您看到config.json文件时,请求的格式如下:
{ "restFqdn":"kuno.dev.net", "restPort":"58085" }
Which isn`t an array.Also why do you need a constructor to make the Call to your config.json file, when you are doing the same in : 这不是一个数组,所以当您在config.json文件中执行相同的操作时,为什么还需要一个构造函数来调用config.json文件:
this._fileService.getConfFile().subscribe (
data => {
All you need to do is following: 您需要做的是:
Create a simple class, which maps with your config request as below (Not compulsory, but it is a cleaner way to read a Config File): 创建一个简单的类,该类与您的配置请求进行如下映射(不是强制性的,但这是读取配置文件的一种更简洁的方法):
export class ConfigProp {
restFqdn: string;
restPort: string;
}
In your FileService class try with following changes: 在您的FileService类中,尝试以下更改:
@Injectable()
export class FileService {
result: ConfigProp;
constructor(private http:Http) {
}
getConfFile() {
return this.http.get('json/conf.json')
.map( (res:Response) => {
this.result = res.json();
console.log('FileService - getConfFile(): ', this.result)
return this.result;
}
)
.catch( error =>
Observable.throw(error || 'Server error')
);
};
}
Rest Everything else seems fine. 休息其他一切似乎都很好。
Also a word of advice. 也是一个忠告。 Since the config file is a shared file among different services.
由于配置文件是不同服务之间的共享文件。 You might need to keep it in assets folder, as the folder is meant to contain shared, configurable resources.
您可能需要将其保留在资产文件夹中,因为该文件夹旨在包含共享的可配置资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.