【问题标题】:Angular 2 - Number to String Conversion using pipeAngular 2 - 使用管道将数字转换为字符串
【发布时间】:2017-02-15 11:43:09
【问题描述】:

我正在尝试使用管道内的打字稿将数字值转换为角度 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',

);
}
}

【问题讨论】:

    标签: angular pipe


    【解决方案1】:

    您的函数要求返回一个数字,而您正在返回一个字符串。试试:

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

    【讨论】:

    • 您能否接受答案并投票,以便其他人可以使用它?
    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多