我正在尝试在我的http代码中使用一些reactiveX.为此,我正在创建一个Subject,并尝试调用onNext函数,但是我收到一条错误,说subject.onNext不是函数.
HTML:
< input #search(input)=“generateSuggestions($event.target.value)”>
TS:
import { Jsonp,Response } from 'angular2/http'; import { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject' import 'rxjs/add/operator/map'; import 'rxjs/add/operator/debounce'; import { Injectable } from 'angular2/core'; @Injectable() export class SearchService { queryStream = new Subject(); generateSuggestions(query: string) { this.queryStream.onNext(query) .debounce(500) .map( query => this.jsonp.request( `http://urlendpoint/${query}` ) .map( (res: Response) => res.json() ) .subscribe( results => console.log(results) ); ) } } var queryStream = new Subject();
ORIGINAL EXCEPTION:TypeError:this.queryStream.onNext不是函数
我的所有普通http函数都工作(this.jsonp.request(url / query).map().subscribe())但是onNext运算符不起作用.我也无法导入flatMap,它似乎与map和debounce不在同一个’rxjs / add / operator / …’文件夹中.
更换
原文链接:https://www.f2er.com/angularjs/141192.htmlimport { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject' import 'rxjs/add/operator/map'; import 'rxjs/add/operator/debounce';
用:
import * as Rx from "rxjs/Rx";
和
用Rx.Subject替换主题
将next替换为next