无法读取未定义的属性“ asObservable”

时间:2019-06-02 15:49:33

标签: angular typescript observable

我正在通过JSON API构建搜索功能,并且出现此错误:

错误:未捕获(承诺):TypeError:无法读取未定义的属性“ asObservable”

我对角度不熟悉,似乎找不到问题。

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

    }

1 个答案:

答案 0 :(得分:0)

您在productService.ts中的主题称为searchResults,但是您在服务中的getSearchResult函数返回this.searchResult(可能是错字)。