为什么我的this._setPropertyName函数不起作用?

时间:2018-07-27 18:39:55

标签: polymer polymer-2.x

我有以下聚合物元素:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../shared-styles.html">
<link rel="import" href="../../bower_components/dfw-styles/dfw-styles.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">

<dom-module id="baseline-policy-create">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
      .top-button{
        float : right;
      }
    </style>

    <div class="outer-buttons">
      <paper-menu-button horizontal-align="right" no-overlap="true" no-animations class="top-button">
        <paper-button slot="dropdown-trigger" class="dropdown-trigger create-button btn-primary">
          <iron-icon icon="menu"></iron-icon>
          <span>Create Baseline Policy</span>
        </paper-button>
        <paper-listbox slot="dropdown-content" class="dropdown-content">
          <template is="dom-repeat" items="{{_domains}}">
            <paper-item on-tap="getDomainSchema">[[item.label]]</paper-item>
          </template>
        </paper-listbox>
      </paper-menu-button>
    </div>
  </template>
  <script>
    class BaselinePolicyCreate extends Polymer.Element {
      static get is() {
        return 'baseline-policy-create';
      }

      static get properties() {
        return {
          _domains: {
            type: Array,
            value: [{'name':'Package', 'label':'Package'},
                    {'name':'Subscription', 'label':'Subscription'}] //TODO: is it possible to get these values from an API source
          },
          requestedDomain: {
            type: String,
            value : "",
            notify : true,
            readOnly : true
          }
        }
      }

      getDomainSchema(evt) {
        console.info("Get the schema for the following domain:", evt.target.innerText);
        console.log(this.requestedDomain);
        this._setRequestedDomain(evt.target.innerText);
        //this.requestedDomain = evt.target.innerText;
        console.log(this.requestedDomain);
      }
    }
    customElements.define(BaselinePolicyCreate.is, BaselinePolicyCreate);
  </script>
</dom-module>

我一直在关注有关数据绑定的本教程:https://www.tutorialspoint.com/polymer/polymer_data_system.htm

在示例中,prop-element的代码具有myProp属性,其属性readOnly设置为true。在Onclick函数中,它仍然可以使用this._setMyProp()来更改属性的值,该值未在任何地方明确定义。

我想在我的代码中做同样的事情。也就是说,我想使用此方法设置requestDomain。我可以使用此行设置它:

this.requestedDomain = evt.target.innerText;

但是要这样做,我不能将readOnly标志设置为true。如果我使用this._setRequestedDomain,则会收到一条错误消息,指出它不是函数。我是否在顶部缺少一些导入功能以允许此功能正常工作,或者可能已在Polymer 2中将其删除?

1 个答案:

答案 0 :(得分:0)

我一直在测试您的代码,没关系。

You only will get the error "this.setRequestedDomain is not a function" if your property is set as readOnly = false.

Please, try again and tell me if its ok.

pen code example (without styling)