Angular2 RC1 Pipes Always Passing Undefined

雨燕双飞 提交于 2019-12-10 18:24:38

问题


After upgrading to RC1 from Beta, pipes don't seem to be correctly passing data. I am receiving the following:

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

This is an old plunker, but it shows the way I am using pipes within my application. https://plnkr.co/edit/DHLVc0?p=preview

Sometimes I get no errors at all and it treats the page as if there were no pipes whatsoever.


回答1:


On version 2.0.0-beta.16, pipes had breaking changes. From angular2 changelog

pipes now take a variable number of arguments, and not an array that contains all arguments.

So, instead of transform(value, args){} it's now transform(value,args...){}

before

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

Now

transform(value, arg1, arg2, arg3)

If you don't want to make any changes to your pipes, you could still use them but you need to change the way you add arguments

Before:

{{someValue|somePipe:arg1:arg2}} 

change it to:

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

Or, the better way, is to change your pipes and make the transform method accepts multiple arguments instead of one array.

Now, going to the plunk on your question:

All you need to make it work with the new versions is to change:

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

To

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

And that's it. Because in your code, you never call the pipe with more than one argument. It's always an array of arguments, that fits perfectly with the new syntax.

Here is your plunk fixed



来源:https://stackoverflow.com/questions/37197293/angular2-rc1-pipes-always-passing-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!