import {Pipe, PipeTransform} from '@angular/core';
import {Product} from "./product";
@Pipe({
name: 'productFilter'
})
export class ProductFilterPipe implements PipeTransform {
transform(value: Product[], filterText?: string): Product[] {
filterText = filterText?filterText.toLocaleLowerCase(): null
return filterText ? value.filter((p: Product) => p.name
.toLocaleLowerCase().indexOf(filterText) !== -1) : value;
}
}
TS2322:鍵入“字串 | null' 不可分配給型別 'string | 不明確的'。
型別 'null' 不能分配給型別 'string | 不明確的'。
和
TS2345:“字串”型別的引數 | undefined' 不能分配給'string' 型別的引數。
型別“未定義”不可分配給型別“字串”。
在此處輸入影像描述
uj5u.com熱心網友回復:
試試這個
import {Pipe, PipeTransform} from '@angular/core';
import {Product} from "./product";
@Pipe({
name: 'productFilter'
})
export class ProductFilterPipe implements PipeTransform {
transform(value: Product[], filterText?: string): Product[] {
let pattern = filterText ? filterText.toLocaleLowerCase(): null;
return pattern ? value.filter((p: Product) => p.name.toLocaleLowerCase().indexOf(pattern) !== -1) : value;
}
}
如果 filterText 未傳遞給函式,那么您會收到該錯誤,因為您試圖將某些內容分配給未定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/475717.html
標籤:javascript 有角度的 打字稿 智能理念
