Angular 2 - 在调用方法之前使用其他服务的服务 [英] Angular 2 - Services consuming others services before call a method
问题描述
我有这个场景.
后端.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 = Subscriber {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
方法类似于:
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屋!