我在这条线上遇到错误 this.apiService.getGifs(searchValue.value).subscribe(数据=> this.results =数据);
我的代码有效,但我收到此错误,所以我想弄清楚如何修复它。我认为这部分属于.subscribe(data=>this.results=data);
部分,但没有代码就行不通。
apiService
@Injectable()
export class ApiService {
private results = new BehaviorSubject([]);
public getResults$(){
return this.results.asObservable();
}
constructor (private http: Http) {}
link = 'http://api.giphy.com/v1/gifs/search?q=';
ApiKey = 'MaklOv0qiTVslRvJlBShgpr46LWPs5Z0';
public getGifs(searchValue:string) {
return this.http.get(this.link + searchValue +'&api_key='+ this.ApiKey)
.map((res:Response) => this.results.next(res.json().data));
}
}
将数据发送到服务的组件
export class SearchComponent implements OnInit {
results = []
constructor(private apiService: ApiService) { }
ngOnInit() {
}
onSearch(searchValue) {
this.apiService.getGifs(searchValue.value).subscribe(data=>this.results=data);
}
}
接收数据的组件
export class OutputComponent implements OnInit {
results = [];
constructor(private apiService: ApiService){
apiService.getResults$().subscribe(data => this.results = data)
}
答案 0 :(得分:1)
除了@jonrsharpe说这应该有效:
import 'rxjs/add/operator/do';
public getGifs(searchValue:string) {
return this.http.get(this.link + searchValue +'&api_key='+ this.ApiKey)
.map((res:Response) => res.json().data)
.do( data => this.results.next(data))
}