我在实现rxjs v6和rxjs / operators时遇到问题:
import { Observable } from 'rxjs';
import { Subject } from 'rxjs';
import { startWith, debounceTime } from 'rxjs/operators';
export class ProductListComponent implements OnInit {
public products$: Observable<Product[]>;
public searchTerm: string = '';
private searchSubject: Subject<string> = new Subject();
private reloadProductsList: Subject<void> = new Subject();
constructor(private productService: ProductService) { }
ng OnInit() {
this.products$ = this.searchSubject
.pipe(startWith(this.searchTerm), debounceTime(300));
}
...
}
this.products$
中的ngOnInit()
是特定错误。这是错误:
Type 'Observable<string | Product[]>' is not assignable to type 'Observable<Product[]>'.
Type 'string | Product[]' is not assignable to type 'Product[]'.
Type 'string' is not assignable to type 'Product[]'
错误让我感到困惑。你能帮忙吗?
是,问题出在startWith(this.searchTerm)
。您不能以此开头,因为它是一个字符串,并且this.products$
为Observable<Product[]>;
。另外,this.searchSubject
是Subject<string>
。
您确定不是this.products$ = this.productService.getProducts();
,是这样吗?