rxjs v6和rxjs / operators的错误

问题描述 投票:-1回答:1

我在实现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[]'

错误让我感到困惑。你能帮忙吗?

angular rxjs
1个回答
0
投票

是,问题出在startWith(this.searchTerm)。您不能以此开头,因为它是一个字符串,并且this.products$Observable<Product[]>;。另外,this.searchSubjectSubject<string>

您确定不是this.products$ = this.productService.getProducts();,是这样吗?

© www.soinside.com 2019 - 2024. All rights reserved.