Angular2 RC1管始终通过未定义

时间:2016-05-12 20:58:29

标签: angular angular2-pipe

从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

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

1 个答案:

答案 0 :(得分:5)

在版本 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方法接受多个参数而不是一个数组。

现在,请回答你的问题:

使用新版本所需的一切就是改变:

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

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

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

Here is your plunk fixed