2016-06-24 76 views
9

我用「angular2的WebPack」「angular2 /形式,可觀察的」,但遇到了一個錯誤,需要幫助..angular2觀察特性 'debouceTime' 上不存在類型 '可觀察<any>'

有一個自定義窗體驗證 -

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}); }; } 
     )});} 

我能找到的文件「/projection_direction/node_modules/rxjs/operator/debounceTime.js」

爲什麼會出現這樣的error--

住宅「debouceTime」上鍵入「可觀測」不存在。

回答

26

確保您已開始在main.ts (其中應用程序被bootstraped)

​​

或一次

import "rxjs/Rx"; 

EXTEND

有是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"); 
    } 
} 

和工作時main.ts我們:

//main entry point 
import {bootstrap} from '@angular/platform-browser-dynamic'; 
import {App} from './app'; 

import "rxjs/Rx"; 

bootstrap(App, []) 
    .catch(err => console.error(err)); 

檢查它here

+0

我添加'進口「rxjs /添加/運營商/地圖」; 現在導入「rxjs/add/operator/debounceTime」; '在main.ts ,,但錯誤仍然... –

+0

我爲你創建了一個plunker,擴展了答案..希望它應該有幫助 –

+0

@RadimKöhler我認爲Plunker是錯誤的。我個人看到TS錯誤 '類型'(term:any)=> void'的參數不能分配給類型爲'(value:any,index:number)'的參數=> ObservableInput <{}>'。 類型'void'不可分配給類型'ObservableInput <{}>'.' – BenRacicot

3

在這裏你有一個錯字。它的debounceTime,而不是debouceTime :)