高分子基础问题

Polymer basic issue

您好,我正在尝试执行一个基本的聚合物程序。在步骤 var el = new HelloElement(); 中获取以下异常。元素本身也没有附加到页面。

异常

Uncaught TypeError: Illegal constructor
    at PropertyAccessors (property-accessors.html:119)
    at TemplateStamp (template-stamp.html:119)
    at PropertyEffects (property-effects.html:1075)
    at PolymerElement (element-mixin.html:459)
    at GestureEventListeners (gesture-event-listeners.html:40)
    at LegacyElement (legacy-element-mixin.html:69)
    at PolymerGenerated (class.html:137)
    at (index):18
    at html-imports.js:580
    at html-imports.js:617

代码

<!DOCTYPE html>
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"> </script>
<link rel="import" href="bower_components/polymer/polymer.html"/>
</head>
  <body>
      <script>
          HTMLImports.whenReady( function () {
          HelloElement = Polymer.Class({
              is: "hello-element",
              created: function () {
                  this.textContent = "Hello World";
              }
          });
          document.registerElement('hello-element', HelloElement);

          var el = new HelloElement();
          document.querySelector("body").appendChild(el);
      })
      </script>
  </body>
</html

尝试使用新的 class 语法如下:

HTMLImports.whenReady(function() {
  class MyElement extends Polymer.Element {
    static get is() { return 'my-element'; }
    static get properties() {
      return {
        prop: {
          type: String
        }
      }
    }
    constructor() {
      super();
      this.prop = 'my-element'
    }
  }
  customElements.define(MyElement.is, MyElement);

  var el = new MyElement();
  el.textContent = 'my-element';
  document.querySelector("body").appendChild(el);
});

JSBin: http://jsbin.com/vefelacada/edit?html,console,output

查看更多信息:https://www.polymer-project.org/2.0/docs/devguide/registering-elements

=== 编辑

如果你真的想使用 Polymer 1.0 语法,你可能应该这样做:(不是 100% 确定错误的真正含义)

  Polymer({
    is: "hello-element",
  });

  var el = document.createElement("hello-element");
  document.querySelector("body").appendChild(el);
  el.textContent = 'my-element';

http://jsbin.com/zihayumaqo/edit?html,console,output