observedAttributes()不是一个函数

问题描述 投票:2回答:1

我有一个JS Custom Element属性

  static get observedAttributes() {
    return ['state','color'];
  }

我怎样才能在自定义元素的Callback函数,整个阵列?

this.observedAttributes()不是一个函数

我必须忽视的东西容易...

在评论的问题后更新:

我总是忘记的Getter和Setter

我现在可以做到这一点在构造函数():

    this.constructor.observedAttributes.map(attribute => 
    Object.defineProperty(this, attribute, {
      get() {
        return obj.getAttribute(attribute);
      },
      set(newValue) {
        obj.setAttribute(attribute, newValue);
      },
      enumerable: true,//default:false
      configurable: true // default:false
      //writable: true // not valid, since there is a set method!
    }));

(不关心内存消耗副作用)

web-component custom-element
1个回答
4
投票

observedAttributes被定义为static所以它会被取消的类,而不是一个实例。 observedAttributes也是getterget),所以你不会()执行它。如果您定义的自定义元素类,你应该使用FancyButton FancyButton.observedAttributes

© www.soinside.com 2019 - 2024. All rights reserved.