Ошибки с rx js v6 и rxjs / операторами - PullRequest
0 голосов
/ 20 марта 2020

У меня проблемы с реализацией rx js v6 и rxjs / operator:

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

Указанная ошибка c связана с this.products$ in 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[]'

Ошибка меня смущает. Вы можете помочь?

1 Ответ

0 голосов
/ 20 марта 2020

Да, проблема с startWith(this.searchTerm). Вы не можете начать с этого, так как это строка, а this.products$ равно Observable<Product[]>;. Кроме того, this.searchSubject - это Subject<string>.

Вы уверены, что это не this.products$ = this.productService.getProducts();, что-то вроде этого?

...