如何在 Google Polymer 2 中访问 'properties'?

How to access 'properties' in Google Polymer 2?

在 Polymer 1.0 中,我可以声明属性:

properties: {
    salary: {
        type: Number,
        value: 80
    }
}

现在在 Polymer 2.0 中(创建一个 class),我应该写一个这样的方法:

static get properties() {
    return {
        salary: {
            type: Number,
            value: 80
        }
    }
}

但现在 this.propertiesnull。如何访问 properties 字段(不是 properties 的值)?

this.properties 尝试访问实例字段(不包括静态字段)。由于 properties 是一个 static 字段,您必须使用 this.constructor.propertiesXFoo.properties.

示例:

class XFoo extends Polymer.Element {
  static get properties() { ... }

  constructor() {
    super();

    // this.constructor === XFoo
    console.log('this.constructor.properties', this.constructor.properties);
    console.log('XFoo.properties', XFoo.properties);
  }
}

codepen