Search code examples
angularrxjsobservableangular2-forms

angular2 Observable Property 'debouceTime' does not exist on type 'Observable<any>'


I use "angular2 webpack" and "angular2/form,Observable" , but met an error ,need help ..

There is a custom form validator --

import {Observable} from 'rxjs/Rx';
import {REACTIVE_FORM_DIRECTIVES,FormControl, FormGroup, Validators} from '@angular/forms';

emailShouldBeUnique(control:FormControl) {
    return new Observable((obs:any)=> {
      control.valueChanges
        .debouceTime(400)
        .distinctUntilChanged()
        .flatMap(term=>return !this.userQuery.emailExist(term))
        .subscribe(res=> {
            if (!res) {obs.next(null)}
            else {obs.next({'emailExist': true}); }; }
        )});}

I could find the file "/projection_direction/node_modules/rxjs/operator/debounceTime.js"

why is there such the error--

Property 'debouceTime' does not exist on type 'Observable'.


Solution

  • Be sure you've initiated that in main.ts (where the app is bootstraped)

    import "rxjs/add/operator/map";
    import "rxjs/add/operator/debounceTime";
    ...
    

    or all at once

    import "rxjs/Rx";
    

    EXTEND

    there is a working example

    //our root app component
    import {Component, EventEmitter, ChangeDetectorRef} from '@angular/core'
    import {Observable} from  "rxjs/Rx";
    @Component({
      selector: 'my-app',
      providers: [],
      template: `
        <div>
          <h2>Hello {{name}}</h2>
    
            <div>debounced message: {{message}}</div>
        </div>
      `, 
      directives: []
    })
    export class App {
    
      protected message: string;
      protected emitter = new EventEmitter<string>();
      public obs: Observable<string>;
    
      constructor() {
        this.name = 'Angular2 (Release Candidate!)'
    
        this.obs = this.emitter
          .map(x => x)
          .debounceTime(1200)
          ;
    
        this.obs.subscribe(msg => this.message = msg);
      }
    
      ngOnInit(){
        this.emitter.emit("hello after debounce");
      }
    }
    

    and that is working when in main.ts we have:

    //main entry point
    import {bootstrap} from '@angular/platform-browser-dynamic';
    import {App} from './app';
    
    import "rxjs/Rx"; 
    
    bootstrap(App, [])
      .catch(err => console.error(err));
    

    Check it here