【发布时间】:2016-11-21 03:18:30
【问题描述】:
我创建了 CustomHttp 类,它像这里一样扩展 Http:http://restlet.com/blog/2016/04/18/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-3/#comment-83563
我像这样将提供程序添加到引导程序中:
bootstrap([
HTTP_PROVIDERS,
{ provide:Http,
useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, errorNotifier: NotificationHandlerService,
authService: AuthInfoService) => {
return new CustomHttp(backend, defaultOptions, errorNotifier, authService);
},
deps: [ XHRBackend, RequestOptions, NotificationHandlerService, AuthInfoService]
},
])
所有被覆盖的方法(get、post 等)都可以正常工作。然后我将自定义属性和方法添加到 CustomHttp 类并尝试访问 CustomHttp 之外的属性:
@Injectable()
export class CustomHttp extends Http {
...
private myCustomProperty = 'It`s custom';
public getCustomProperty() {
return this.myCustomProperty;
}
...
}
======================
import {Http} from '@angular/http';
export class MainComponent {
constructor(private _http: Http) {
this._http.getCustomProperty(); // this method doesn`t exist in Http
}
}
如何访问 CustomHttp 的自定义方法和属性?
【问题讨论】:
标签: typescript angular