聚合物数据绑定:在路径中使用属性



是否可以在路径中使用属性(下面标记的属性(?

<template is="dom-if" if="[[one.two.PROPERTY.four]]">
 ...
</template>
class MyTag extends Polymer.Element {
  static get is() {return 'my-tag'}
  static get properties() {
    return {
      PROPERTY: String
    }
  }
}
customElements.define(MyTag.is, MyTag)
<dom-module id="my-tag">
  <template>
    <template is="dom-if" if="{{_ifFour}}">
      <b>yo</b>
    </template>
  </template>
  <script>
  class TestElement extends Polymer.Element {
    static get is() { return 'my-tag' }
    static get properties () {
      return {
        PROPERTY: {
          type: String,
          value: 'initial',
          observer: '_PROPERTYChanged'
        },
        one: {
          type: Object,
          value: function () {
            return {
              two: {}
            }
          }
        },
        _ifFour: {
          type: Boolean,
          value: false
        }
      }
    }
    static get observers () {
      return ['_computeIfFour(one.*)']
    }
    _computeIfFour () {
      if (this.one.two[this.PROPERTY] === undefined)
        this._ifFour = false;
      this._ifFour = this.one.two[this.PROPERTY].four;
    }
    _PROPERTYChanged (propertyName, old) {
      if (old) {
        delete this.one.two[old];
      }
      this.one.two[propertyName] = {
        four: ''
      }
    }
  }
  customElements.define(MyTag.is, MyTag)
  </script>
</dom-module>

实际上有一种工作方式。但这太多了, hacky 您不应该在生产代码中使用它。如果共享的话,这是总的,如果您的代码可能会增长,那么您可能会对它产生不希望的副作用。

最新更新