【问题标题】:Angular2 RC1 Pipes Always Passing UndefinedAngular2 RC1 管道总是通过未定义
【发布时间】:2016-09-08 21:36:42
【问题描述】:

从 Beta 版升级到 RC1 后,管道似乎无法正确传递数据。我收到以下信息:

ORIGINAL EXCEPTION: TypeError: Cannot read property '1' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property '1' of undefined

这是一个旧的 plunker,但它显示了我在应用程序中使用管道的方式。 https://plnkr.co/edit/DHLVc0?p=preview

有时我根本没有收到任何错误,它会将页面视为没有任何管道。

【问题讨论】:

    标签: angular angular2-pipe


    【解决方案1】:

    2.0.0-beta.16 版本中,管道发生了重大变化。来自angular2 changelog

    管道现在接受可变数量的参数,而不是包含所有参数的数组。

    所以,现在不是transform(value, args){},而是transform(value,args...){}

    之前

    transform(value, [arg1,arg2,arg3])
    

    现在

    transform(value, arg1, arg2, arg3)
    

    如果您不想对管道进行任何更改,您仍然可以使用它们,但您需要更改添加参数的方式

    之前:

    {{someValue|somePipe:arg1:arg2}} 
    

    改成:

    {{someValue|somePipe:[arg1,arg2]}} // this will work with the new syntax without changing anything in the pipe itself
    

    或者,更好的方法是更改​​管道并使转换方法接受多个参数而不是一个数组。

    现在,直接回答你的问题:

    要让它与新版本一起使用,您只需进行更改:

    transform(input:any, [config = '+']): any{
    

    transform(input:any, config = '+'): any{
    

    就是这样。因为在您的代码中,您永远不会使用多个参数调用管道。它始终是一个参数数组,与新语法完美契合。

    Here is your plunk fixed

    【讨论】:

    • 完美,谢谢!~ 我只看了 RC0 和 RC1 的更新日志,呵呵
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多