首页 文章

Polymer Build找不到非聚合物元素的ES6类

提问于
浏览
2

我们正在使用Polymer 2构建应用程序 . 我们有源自Polymer Elements的Web Components和处理业务逻辑的纯ES6类 . 我们在html文件中定义了ES6类,并通过html import将它们导入到应该使用类的位置 .

myclass.html

<script>
    class MyClass {

        constructor(){
            this.text = "MyClass";
        }

        getText(){
            return this.text;
        }
    }
</script>

我-app.html

<link rel="import" href="../bower_components/polymer/polymer-element.html">

<link rel="import" href="myclass.html">

<dom-module id="my-app">
  <template>
    <div>{{text}}</div>
  </template>

  <script>
    class MyApp extends Polymer.Element {
      static get is() { return 'my-app'; }

      static get properties() {
        return {};
      }

      constructor(){
        super();
        var myObject = new MyClass();
        this.text = myObject.getText();
      }
    }

    window.customElements.define(MyApp.is, MyApp);
  </script>
</dom-module>

通过源提供聚合物,这是完美的 .

但是当我们进行Polymer Build并通过构建运行时,会出现以下错误 .

my-app.html:1 Uncaught ReferenceError: MyClass is not defined
    at new MyApp (my-app.html:1)
    at my-app.html:1

1 回答

  • 0

    这可以通过修改MyClass来解决,如下所示 . 添加静态get属性方法 .

    <script>
        class MyClass {
    
            constructor(){
                this.text = "MyClass";
        }
    
        static get properties() {
            return {
            };
        }
    
        getText(){
            return this.text;
        }
    }
    

相关问题