如何将参数传递给Polymer中的函数

时间:2017-01-10 20:11:33

标签: javascript polymer

我试图调用一个函数并传递一个参数,但是它给出了一个错误,即没有定义函数。我认为这不是传递论证的正确方法。



//it's a big component with many methods

updateData: function(id) {
  console.log(id);
  }

<div class="basic">
  <preview-list class="profileList">
    <template is="dom-repeat" items="{{ followers }}">
      <preview-profile profile$="{{ item }}">
        <div class="connectWrapper">
          <template is="dom-if" if="{_computeIsNotConnected(item.isConnection, item.isConnection2)}}">
            <custom-button on-tap="[[updateData(item.id)]]"></custom-button>
          </template>
        </div>
      </preview-profile>
    </template>
  </preview-list>
</div>
&#13;
&#13;
&#13;

它显示以下错误: listener method `[[updateData(item.id)]]` not defined 我也尝试过花括号。 我怎样才能将参数传递给函数?

2 个答案:

答案 0 :(得分:2)

您必须使用函数的名称并从事件参数中获取对象:

pickledegg> g++ -std=c++11 -o shared_ptr shared_ptr.cpp
shared_ptr.cpp:29:4: error: no matching member function for call to 'push_back'
        w.push_back(new int(7));
        ~~^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:697:36: note: 
      candidate function not viable: no known conversion from 'int *' to 'const value_type' (aka
      'const std::__1::shared_ptr<int>') for 1st argument
    _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:699:36: note: 
      candidate function not viable: no known conversion from 'int *' to 'value_type' (aka
      'std::__1::shared_ptr<int>') for 1st argument
    _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
                                   ^
1 error generated.

并定义您的事件处理程序:

<template is="dom-repeat" items="{{ data }}">
  <custom-button on-tap="updateData"></custom-button>
</template>

<强>更新

因为你正在使用dom-if在dom-repeat中,这是一个bug(https://github.com/Polymer/polymer/issues/2574

你可以使用 hidden $ =“{{_ computeIsNotConnected(item.isConnection,item.isConnection2)}}”而不是dom-if模板,

或者改为使用此代码段:

updateData: function(event) {
  console.log(event.model.item.id);
}

答案 1 :(得分:1)

Polymer不希望你在这里传递参数。相反,您应该通过模型... <template id="repeater" is="dom-repeat" items="{{ data }}"> <custom-button on-tap="updateData"></custom-button> </template> updateData: function(event) { console.log(this.$.repeater.modelForElement(event.target)); } 访问所需的数据。有关详情,请参阅此答案:https://stackoverflow.com/a/40205828/2718998