[英]TypeScript Error with angular2 : string is not assignable
我在為過濾器編寫代碼時在Angular2中創建自定義管道,但出現以下構建錯誤:
TS2322:無法將Build:Type'()=> string'分配給類型'string'
請在下面找到我的示例代碼:
import { PipeTransform, Pipe } from 'angular2/core';
import { IProduct } from './products';
@Pipe({
name: 'productFilter'
})
export class ProductFilterPipe implements PipeTransform {
transform(value: IProduct[], args: string[]): IProduct[] {
let filter: string = args[0].toLocaleLowerCase ? args[0].toLocaleLowerCase : null;
return filter ? value.filter((product: IProduct) =>
product.productName.toLocaleLowerCase().indexOf(filter) != -1) : value;
}
}
上面的錯誤來自以下代碼行:
let filter:
我是打字稿新手,有人可以幫助我解決此問題嗎?
您需要在函數toLocaleLowerCase的調用中添加括號。 換句話說,您應該做到:toLocaleLowerCase()
因為當您在不帶括號的情況下提供函數名稱時,您並不是真正在調用該函數。 您是說要使變量過濾器等於函數本身,而不是等於調用函數后從函數返回的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.