Angular2 RC1管道始终通过未定义 [英] Angular2 RC1 Pipes Always Passing Undefined

查看:88
本文介绍了Angular2 RC1管道始终通过未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

的属性'1'它显示了我在应用程序中使用管道的方式。 https://plnkr.co/edit/DHLVc0?p=preview

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.

推荐答案

在版本 2.0.0-beta.16 中,管道发生了重大更改。来自 angular2更改日志

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.

因此,现在是 transform(value,args ...){} transform(value,args){} c>

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

之前

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

现在

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

之前:

{{someValue|somePipe:arg1:arg2}} 

将其更改为:

{{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.

这里是您的小酒瓶

这篇关于Angular2 RC1管道始终通过未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆