简体   繁体   English

如何在Polymer 2.0 on-tap功能中传递参数?

[英]How to pass parameters in Polymer 2.0 on-tap function?

I'm using Polymer 2.0 and I have a dom-repeat for different to-do cards. 我正在使用Polymer 2.0,我有一个dom-repeat用于不同的待办事项卡。 What I want to do is remove the card when I click on it. 我想要做的是点击它时取出卡。

So I tried this on-tap=deleteNote([[index]]) which uses the index from the dom-repeat . 所以我尝试了这个on-tap=deleteNote([[index]]) ,它使用了dom-repeat的索引。 However Polymer doesn't execute the function. 但是Polymer不执行该功能。

What am I doing wrong? 我究竟做错了什么?

Another solution could be the dataset object within the event.target . 另一个解决方案可能是event.targetdataset对象。 You can define your properties with the data- prefix: 您可以使用data-前缀定义属性:

<div on-tap="doSomething" data-item$="[[item]]"></div>

And within your doSomething() listener you can get the dataset object: doSomething()侦听器中,您可以获取dataset对象:

doSomething(event) {
  const item = event.target.dataset.item;
  ...
}

You can access the model via the event argument's model property. 您可以通过event参数的model属性访问model

So you can access the index from event.model.index . 因此,您可以从event.model.index访问索引。

Well, I am aware you can't. 好吧,我知道你做不到。 There are many discussions about that on the internet. 互联网上有很多关于此的讨论。

Of course, there is a way how to pass argument to function. 当然,有一种方法可以将参数传递给函数。 You can save [[index]] in attribute of element and then get the attribute when needed. 您可以在元素的属性中保存[[index]] ,然后在需要时获取属性。

Example: 例:

<div on-tap='_deleteNote' indexed$='[[index]]'>

Then in script: 然后在脚本中:

_deleteNote(e) {
  var index = e.target.getAttribute('indexed');
  ...
}

Once you get index , you can do whatever you want with it. 获得index ,您可以随心所欲地执行任何操作。

Don't forget to extend Polymer gestures if you are not using hybrid elements in Polymer 2.0. 如果不在Polymer 2.0中使用混合元素,请不要忘记扩展Polymer手势。

class Foo extends Polymer.GestureEventListeners(Polymer.Element) {}

More about it: https://www.polymer-project.org/2.0/docs/devguide/gesture-events#using-gesture-events 更多信息: https//www.polymer-project.org/2.0/docs/devguide/gesture-events#using-gesture-events

What is hybrid element: https://www.polymer-project.org/2.0/docs/devguide/hybrid-elements 什么是混合元素: https//www.polymer-project.org/2.0/docs/devguide/hybrid-elements

on-tap does not appear to be implemented in Polymer 2.0. on-tap似乎没有在Polymer 2.0中实现。 If you instead use on-click then it will work. 如果您改为使用on-click那么它将起作用。

EDIT: see below comment. 编辑:见下面的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM