【问题标题】:Import js library in polymer 2.0在聚合物 2.0 中导入 js 库
【发布时间】:2019-01-14 06:55:28
【问题描述】:

我遇到了一个问题,我想知道如何将外部 javascript“库/文件”导入到我的聚合物项目中。 我想在聚合物中使用 htmlToPdf.js 库。但我无法调用 htmlToPdf.js 上的函数。

【问题讨论】:

    标签: javascript polymer polymer-2.x polymer-starter-kit


    【解决方案1】:

    如果你打算在不同的组件中复用这个库,那么最好创建一个导入组件

    <!-- htmlToPdf-import.html -->
    <script src="../htmlToPdf/htmlToPdf.js" charset="utf-8"></script>
    
    <!-- my-component.html -->
    
    <link rel="import" href="./htmlToPdf-import.html">
    
    <dom-module id="my-component">
      <template>
      </template>
    
      <script>
        class MyComponent extends Polymer.Element {
          static get is() {
            return 'my-component';
          }
        }
    
        window.customElements.define(MyComponent.is, MyComponent);
      </script>
    </dom-module>
    

    【讨论】:

      【解决方案2】:

      您可以使用常规的&lt;script&gt; 标签将其导入到 Polymer 组件中。

      【讨论】:

      • 在元素中使用脚本标签来进行函数调用会给出“不是函数”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      相关资源
      最近更新 更多