扩展http类并访问自定义属性(Angular2打字稿)

诺维尼

我创建了CustomHttp类,该类扩展了Http,例如:http ://restlet.com/blog/2016/04/18/interacting-ficiently-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的自定义方法和属性?

蒂埃里圣堂武士

您可以通过将Http实例强制转换为尝试以下操作CustomHttp

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { CustomHttp } from './custom.http';

@Injectable()
export class BooksService {

  constructor(private http:Http) {
    console.log((<CustomHttp>this.http).someProperty);
  }

  (...)
}

具有以下CustomHttp类别:

@Injectable()
export class CustomHttp extends Http {
  someProperty:string = 'some string';

  (...)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

什么是angular2打字稿和angular2 ecma脚本

来自分类Dev

组件未渲染Angular 2打字稿

来自分类Dev

angular 2打字稿中的成员属性和构造函数的语法

来自分类Dev

在另一个导航组件中使用Angular2打字稿组件

来自分类Dev

在angular2打字稿方法中指定返回类型

来自分类Dev

解析器错误:表达式的意外结尾:{{value?}} | Angular 2打字稿

来自分类Dev

何时在Angular2打字稿中创建构造函数?

来自分类Dev

Angular 2打字稿调用JavaScript函数

来自分类Dev

Angular2打字稿拖放功能

来自分类Dev

如何在angular 2中包含打字稿自定义类型定义?

来自分类Dev

在angular2打字稿应用程序中使用聚合物纸对话

来自分类Dev

Angular2中的自定义值访问器

来自分类Dev

angular2获取自定义属性的值

来自分类Dev

Angular 2打字稿@injectable

来自分类Dev

如何在Angular2 RC5打字稿中调用共享指令的方法?

来自分类Dev

依赖注入抽象类打字稿(Angular2)

来自分类Dev

Angular 2打字稿调用JavaScript函数

来自分类Dev

设置angular2打字稿环境时遇到问题

来自分类Dev

angular2打字稿未编译,但在运行时有效

来自分类Dev

在angular2打字稿应用程序中使用聚合物纸对话

来自分类Dev

Angular2中的自定义值访问器

来自分类Dev

扩展http类并访问自定义属性(Angular2打字稿)

来自分类Dev

Angular2 Raw HTML自定义属性

来自分类Dev

直接从ionic2打字稿组件访问Cordova插件

来自分类Dev

webpack会将Angular2打字稿编译为javascript吗?

来自分类Dev

angular2中的打字稿,如何通过此访问类变量

来自分类Dev

扩展http类用于自定义用法ionic2 / Angular2导致错误

来自分类Dev

未找到属性 angular2 和打字稿

来自分类Dev

在 Angular2 打字稿类中找不到模块

Related 相关文章

热门标签

归档