[英]pipe error : Type 'number' is not assignable to type 'string'
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'concat',
pure: true,
})
export class ConcatPipe implements PipeTransform {
public transform(value: string, args: string[]): string {
return value?.concat(...args);
}
}
When I try to concating in ngFor:当我尝试在 ngFor 中连接时:
<mat-option *ngFor="let name of names$ | async; index as i" [value]="i" [id]="'firstName' | concat: [i]">
I have this pipe where I concat things in the template side, but i get this error:我有这个 pipe 我在模板端连接东西,但我得到这个错误:
Type 'number' is not assignable to type 'string'
How can I fix or extend my pipe?如何修复或扩展我的 pipe?
It means you receive numbers, not strings.这意味着您收到的是数字,而不是字符串。 You should first try to log what happens in
value
and args
.您应该首先尝试记录
value
和args
中发生的情况。 Then you would have to cast numbers into string:然后你必须将数字转换成字符串:
let strValue: string;
if (typeof value === 'number') {
strValue = parseInt(value);
} else {
strValue = value;
}
same goes for the args array: args 数组也是如此:
argsStrs: string[] = args.map(arg => {
if (typeof arg === 'number') {
return parseInt(arg);
} else {
return arg;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.