我想在服务类中创建一个子类,在其中可以声明具有相同名称但用法不同的函数。
我希望能够写:
httpWrapper.get //default is observables. returns observable
httpWrapper.promise.get //returns promise-variant
我当前的服务:
export class HttpWrapperService {
constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
public get<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.get<T>(endpoint, options);
}
public post<T>(endpoint: string, data: any, options?: any): Observable<HttpEvent<T>> {
return this.http.post<T>(endpoint, data, options);
}
public delete<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.delete<T>(endpoint, options);
}
}
export namespace HttpWrapperService {
export class Promise {
constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
public get<T>(endpoint: string, options?: any) {
return this.http.get<T>(endpoint, options).toPromise();
}
public post<T>(endpoint: string, data: any, options?: any) {
return this.http.post<T>(endpoint, data, options).toPromise();
}
public delete<T>(endpoint: string, options?: any) {
return this.http.delete<T>(endpoint, options).toPromise();
}
}
}
但是,当我写httpWrapper.
时,我只能得到可观察到的变体。我不能选择承诺变量。
我该怎么做?
我基本上希望在输入以下内容时显示出智能提示:httpWrapper.
:
httpWrapper.post
httpWrapper.get
httpWrapper.delete
httpWrapper.promise
当我选择httpWrapper.promise.
时:
httpWrapper.promise.get
httpWrapper.promise.post
httpWrapper.promise.delete
答案 0 :(得分:2)
我这样解决了:
@Injectable()
export class HttpWrapperService {
constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
public get<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.get<T>(endpoint, options);
}
public post<T>(endpoint: string, data: any, options?: any): Observable<HttpEvent<T>> {
return this.http.post<T>(endpoint, data, options);
}
public delete<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.delete<T>(endpoint, options);
}
// tslint:disable-next-line:max-classes-per-file
public promise = new class {
constructor(private wrapper: HttpWrapperService) { }
public get<T>(endpoint: string, options?: any) {
return this.wrapper.get<T>(endpoint, options).toPromise();
}
public post<T>(endpoint: string, data: any, options?: any) {
return this.wrapper.post<T>(endpoint, data, options).toPromise();
}
public delete<T>(endpoint: string, options?: any) {
return this.wrapper.delete<T>(endpoint, options).toPromise();
}
}(this);
}
我现在可以使用以下命令调用这些函数:
//different service
constructor(@Inject(HttpWrapperService) private readonly httpWrapper: HttpWrapperService) {}
public letsUseHttpRequests(machine: Machine){
this.httpWrapper.post<Machine>(endpoint, machine); //observable
this.httpWrapper.get<Machine>(endpoint); //observable
this.httpWrapper.delete<Machine>(endpoint); //observable
this.httpWrapper.promise //classContainer
this.httpWrapper.promise.get<Machine>(endpoint); //promise
this.httpWrapper.promise.post<Machine>(endpoint, machine); //promise
this.httpWrapper.promise.delete<Machine>(endpoint); //promise
}
答案 1 :(得分:0)
您可以在network.on( 'doubleClick', function(properties) {
// selected edge id
var edgeId = properties.edges[0];
// find corresponding edge
var edge = edges.get( edgeId );
if ( edge ) {
// found one!
var from = edge.from;
var to = edge.to;
}
});
上使用toPromise
方法。
就是这样。
Observable<T>
httpWrapper.get //default is observables. returns observable