Angular 2-在调用方法之前消耗其他服务的服务 [英] Angular 2 - Services consuming others services before call a method
问题描述
我有这种情况.
backend.json
backend.json
{
"devServer": "http://server1/'",
"proServer" : "http://server2/'",
"use" : "devServer"
}
global.service.ts
global.service.ts
import {Injectable} from '@angular/core';
import {Http, HTTP_PROVIDERS, Response, RequestOptions, URLSearchParams} from '@angular/http';
@Injectable()
export class GlobalService {
constructor(private _http: Http){}
getBackendServer(){
return this.server = this._http.get('./backend.json')
.map((res:Response) => res.json())
}
}
我还有其他服务:search.service.ts
And I've this other service: search.service.ts
import {Injectable} from '@angular/core';
import {Http, HTTP_PROVIDERS, Response, RequestOptions, URLSearchParams} from '@angular/http';
import {GlobalService} from '../services/global.service';
import 'rxjs/add/operator/map';
@Injectable()
export class SearchService {
server;
constructor( private _http: Http, private gs: GlobalService ) {
this.server = gs.getBackendServer().subscribe(
(data) => {
this.server = data[data.use];
},
(err) => {
console.log(err);
}
);
}
DoGeneralSearch(params:Search){
console.log(this.server);
let options = this.buildOptions(params);
return this._http.get(this.server + 'room/search', options)
.map((res:Response) => res.json())
}
}
我想做的事情:看起来很明显:我想将有关后端服务器URL的信息保存在JSON文件中-全局服务应返回服务器,以便可以在搜索服务的任何方法中使用
What I want to do: Looks obvious: I want to keep the information regarding the URL of the backend server in a JSON file - And the global service should return the server so this can be used in any method of the search service.
问题是:DoGeneralSearch()方法在全局服务能够解析任务以读取JSON文件并返回结果之前执行.因此,我有一个this.server =订阅服务器{isUnsubscribed:false,syncErrorValue:null,syncErrorThrown:false,syncErrorThrowable:false,isStopped:false…},而不是结果本身.
The problem is: the DoGeneralSearch() method executes before the global service is capable to resolve the task to read the JSON file and return the result. So, I've a this.server = Subscriber {isUnsubscribed: false, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false…} instead the result itself.
我需要找到一些方法来防止在解析完this.server变量后立即执行方法DoGeneralSearch.
I need to find some how to prevent the method DoGeneralSearch be executed just after resolved the this.server variable.
有什么建议吗?
推荐答案
我认为您可以在应用程序启动时预先加载此类提示.为此,您可以利用APP_INITIALIZER
服务.在实际启动之前,应用程序将等待返回的诺言得到解决.
I think that you could preload such hints at the application startup. For this, you could leverage the APP_INITIALIZER
service. The application will wait for the returned promise to be resolved before actually starting.
以下是示例:
provide(APP_INITIALIZER, {
useFactory: (service:GlobalService) => () => service.load(),
deps:[GlobalService, HTTP_PROVIDERS], multi: true
})
load
方法想要这样的东西:
The load
method would like something like that:
load():Promise<Site> {
var promise = this.http.get('config.json').map(res => res.json()).toPromise();
promise.then(config => this.devServer = config.devServer);
return promise;
}
然后,您可以直接(同步)使用devServer
...
Then you can directly use the devServer
(synchronously)...
有关更多详细信息,请参见github上的此问题:
See this issue on github for more details:
这篇关于Angular 2-在调用方法之前消耗其他服务的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!