angular - rxjs v6 和 rxjs/operators 的错误
问题描述
我在实现 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$
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[]'
这个错误让我很困惑。你能帮忙吗?
解决方案
是的,问题出在startWith(this.searchTerm)
. 你不能从那开始,因为它是一个字符串并且this.products$
是Observable<Product[]>;
. 还有,this.searchSubject
是Subject<string>
。
你确定不是this.products$ = this.productService.getProducts();
这样的吗?
推荐阅读
- dotnetnuke - 如何修复在第三方 DNN 模块上单击“编辑”时发生变化的皮肤?
- python - 熊猫找到最小日期(NOT DATETIME)列
- validation - 在 Firebase 云功能中验证存储文件数据
- html - 如何将此 div 提升到顶部?
- javascript - mup 部署的应用程序中的 .meteor 目录在哪里?
- python - 从python中的范围计算用户输入数字
- c++ - C++升级到VS2017:关于模糊调用的错误C2668
- docker - CRON 作业找不到在 docker-compose 中设置的环境变量
- liferay - 在 Liferay DXP 中部署时出现 Class Cast 异常
- azure-devops - 将 Azure Active Directory 连接到 VSTS 失败