“(string | undefined)[]”类型的参数不可分配给 'string[]' 类型的参数
Argument of type '(string | undefined)[]' is not assignable to parameter of type 'string[]'
我有一些代码但是 angular 13 string[]
出错
'(string | undefined)[]' 类型的参数不可分配给 'string[]'.
类型的参数
在我的产品列表组件中
ngOnInit(): void {
this._getPro();
this._getCate();
}
private _getPro(categoriesFilter?: string[]) {
this.proService.getProducts(categoriesFilter).subscribe((pros) => {
this.products = pros;
});
}
private _getCate() {
this.catService.getCategories().subscribe((cate) => {
this.categories = cate;
});
}
categoryFilter() {
const selectedCategories = this.categories
.filter((cate) => cate.checked)
.map((cate) => cate.id);
this._getPro(selectedCategories);
}
}
在我的产品服务中
//get all
getProducts(categoriesFilter?: string[]): Observable<Product[]> {
let params = new HttpParams();
if (categoriesFilter) {
params = params.append('categories', categoriesFilter.join(','));
}
return this.http.get<Product[]>(this.apiURLProducts, { params: params });
}
this error here
你可以再添加一个过滤条件,保证每个分类都设置了id
属性,过滤函数为type guard predicate function:
categoryFilter() {
const selectedCategories = this.categories
.filter((c): c is typeof c & {
checked: true;
id: string;
} => Boolean(c.checked && c.id))
.map(({id}) => id);
this._getPro(selectedCategories);
}
我有一些代码但是 angular 13 string[]
出错'(string | undefined)[]' 类型的参数不可分配给 'string[]'.
类型的参数在我的产品列表组件中
ngOnInit(): void {
this._getPro();
this._getCate();
}
private _getPro(categoriesFilter?: string[]) {
this.proService.getProducts(categoriesFilter).subscribe((pros) => {
this.products = pros;
});
}
private _getCate() {
this.catService.getCategories().subscribe((cate) => {
this.categories = cate;
});
}
categoryFilter() {
const selectedCategories = this.categories
.filter((cate) => cate.checked)
.map((cate) => cate.id);
this._getPro(selectedCategories);
}
}
在我的产品服务中
//get all
getProducts(categoriesFilter?: string[]): Observable<Product[]> {
let params = new HttpParams();
if (categoriesFilter) {
params = params.append('categories', categoriesFilter.join(','));
}
return this.http.get<Product[]>(this.apiURLProducts, { params: params });
}
this error here
你可以再添加一个过滤条件,保证每个分类都设置了id
属性,过滤函数为type guard predicate function:
categoryFilter() {
const selectedCategories = this.categories
.filter((c): c is typeof c & {
checked: true;
id: string;
} => Boolean(c.checked && c.id))
.map(({id}) => id);
this._getPro(selectedCategories);
}