首页 > 解决方案 > 为什么我的 this._setPropertyName 函数不起作用?

问题描述

我有以下聚合物元素:

<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 中删除?

标签: polymerpolymer-2.x

解决方案


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

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.

笔代码示例(无样式)


推荐阅读