首页 文章

Angular 2 - 使用管道进行字符串转换的数字

提问于
浏览
1

我正在尝试使用管道中的typescript将数字值转换为角度为2的字符串 . 它抱怨

类型字符串不能分配给类型编号

. 我的管道如下 .

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'pxsuffix'

}) export class pxsuffix implements PipeTransform {

transform(input: number): number {

if ((input > 0)) {
    input = input.toString(),
}

return (
    input = input + 'px',

);
}
}

1 回答

  • 4

    您的函数要求返回一个Number并返回一个String . 尝试:

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({ name: 'pxsuffix'
    
    }) export class pxsuffix implements PipeTransform {
    
    transform(input: number): string{ //string type
       return input + 'px';
    } }
    

相关问题