类型'SnapshotAction <unknown> []'不可分配给类型'{title:string;

时间:2020-07-27 20:45:38

标签: angular typescript observable

我正在尝试按角度进行搜索输入。但它显示此错误:

core.js:4196 ERROR TypeError: Cannot read property 'filter' of undefined
    at AdminProductsComponent.filter (admin-products.component.ts:22)
    at AdminProductsComponent_Template_input_keyup_4_listener (admin-products.component.html:7)
    at executeListenerWithErrorHandling (core.js:14414)
    at wrapListenerIn_markDirtyAndPreventDefault (core.js:14449)
    at HTMLInputElement.<anonymous> (platform-browser.js:582)
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:27135)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)

也说类型'SnapshotAction []'不能分配给类型'{title:string; } []' 代码如下

export class AdminProductsComponent implements OnInit, OnDestroy {
  products: {title: string}[];
  filteredProducts: any[];
  subscription: Subscription;

  constructor(private productService: ProductService) { 
    this.subscription = this.productService.getAll().snapshotChanges()
      .subscribe(products => this.filteredProducts = this.products = products); // error here
  }

  filter(query: string){
    this.filteredProducts = (query) ? 
      this.products.filter(p => p.title.toLowerCase().includes(query.toLowerCase())) : //error here
      this.products;
  }

  ngOnDestroy(){
    this.subscription.unsubscribe();
  }

  ngOnInit(): void {
  }

}

0 个答案:

没有答案