Angular2 Timeout运算符不适用于扩展Http

时间:2016-10-13 05:35:33

标签: angular rxjs rxjs5

当我使用rxjs扩展Http类以添加超时设置器时,timeout运算符在我导入它时不起作用import 'rxjs/add/operator/timeout'

仅当我加载整个rxjs库

时才有效

live plnkr

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {Observable} from 'rxjs/Observable'
import 'rxjs/add/operator/timeout'
import {HttpModule, RequestOptions, XHRBackend} from "@angular/http";

import {WpHttp} from "../wpHttp";

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor(private wpHttp: WpHttp) {
    //this.name = 'Angular2'
  }

  ngOnInit(){
    this.wpHttp.get("../test.json").subscribe((res)=>{
      this.name = res.title;
    });
  }
}

@NgModule({
  imports: [ BrowserModule, HttpModule ],
  declarations: [ App ],
  providers: [
    {
      provide: WpHttp,
      useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) =>
        new WpHttp(backend, defaultOptions),
      deps: [XHRBackend, RequestOptions]

    }
  ],
  bootstrap: [ App ]
})
export class AppModule {}

扩展Http

import {Injectable} from '@angular/core';
import {Http, RequestOptions, RequestOptionsArgs, ConnectionBackend} from '@angular/http';

import {Observable} from 'rxjs/Observable';


@Injectable()
export class WpHttp extends Http {

  constructor(backend: ConnectionBackend,
              defaultOptions: RequestOptions) {
    super(backend, defaultOptions);
  }


  get(url): Observable<any> {

    console.log("started")
    return super.get(url)

      .timeout("500", new Error('delay exceeded'))
      .catch((err) => {
        return Observable.throw(err);
      })
      .finally(() => {
        console.log("finished")
      });
  }


}

1 个答案:

答案 0 :(得分:2)

通过添加所有使用的运算符来修复它

SQL> select f_numbertoword(9) from dual;

F_NUMBERTOWORD(123)
--------------------------------------------------------------------------------
 one two three