2016-06-24 28 views
9

tôi sử dụng "angular2 webpack""angular2/form, Quan sát", nhưng gặp lỗi, cần giúp đỡ ..angular2 Quan sát tài sản 'debouceTime' không tồn tại trên loại 'Quan sát <any>'

có một hình thức tùy chỉnh 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}); }; } 
     )});} 

tôi có thể tìm tập tin "/projection_direction/node_modules/rxjs/operator/debounceTime.js"

tại sao có lỗi như vậy--

Thuộc tính 'debouceTime' không tồn tại trên loại 'Quan sát'.

Trả lời

26

Hãy chắc chắn bạn đã thực hiện trong main.ts (nơi ứng dụng được bootstraped)

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

hoặc tất cả cùng một lúc

import "rxjs/Rx"; 

EXTEND

có là 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"); 
    } 
} 

và đang làm việc trong khi main.ts ta có:

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

import "rxjs/Rx"; 

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

Kiểm tra nó here

+0

tôi thêm 'import" rxjs/add/operator/map "; nhập "rxjs/add/operator/debounceTime" ngay bây giờ; 'trong main.ts ,, nhưng lỗi vẫn là ... –

+0

Tôi tạo ra một plunker cho bạn, mở rộng câu trả lời .. hy vọng nó sẽ giúp –

+0

@ RadimKöhler Tôi nghĩ rằng Plunker là lỗi. Cá nhân tôi thấy lỗi TS 'Đối số của loại '(thuật ngữ: bất kỳ) => void' không thể gán cho tham số kiểu '(giá trị: bất kỳ, chỉ số: số) => ObservableInput <{}>'. Loại 'void' không được gán để nhập 'ObservableInput <{}>' .' – BenRacicot

3

Bạn có một typo ở đây. Đó là debounceTime, không phải là debouceTime :)

Các vấn đề liên quan