我正在通过JSON API构建搜索功能,并且出现此错误:
我对角度不熟悉,似乎找不到问题。
productService.ts
import { Observable, of, Subscription, Subject } from "rxjs";
searchResults = new Subject<Product[]>();
searchProduct(term: string): Subscription {
console.log('Service is searching: ' + term );
return this.http.get<Product[]>(this.searchURL + term).subscribe(
results => this.searchResult.next(results));
}
getSearchResult(): Observable<Product[]> {
return this.searchResult.asObservable();
}
component.ts
searchResult: Product[];
constructor(
private productService:ProductService,
private router: Router
) {
this.productService.getSearchResult().subscribe(results => {
this.searchResult = results;
})
}
答案 0 :(得分:0)
您在productService.ts中的主题称为searchResults
,但是您在服务中的getSearchResult
函数返回this.searchResult
(可能是错字)。