javascriptpolymerpolymer-2.xpolymer-starter-kit

Import js library in polymer 2.0


I'm facing an issue I want to know how can I import an external javascript "library/ file" to my polymer project. I want to use the htmlToPdf.js library in polymer. but I can't call the functions on the htmlToPdf.js.


Solution

  • If you are going to reuse the library in different components, then it is better to create an import component

    <!-- 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>