How to include a CDN to VueJS CLI without NPM or Webpack?

Fabricio Chacon picture Fabricio Chacon · Mar 16, 2018 · Viewed 32.1k times · Source

I'm new on VueJS ans Webpack. I've created a project with VueJS CLI and trying to work with it. I need to insert an CDN to my code.

When working with standard HTML, CSS & JS solutions, I'd include CDNs like this:

As you can see, you can add a CDN script with the HTML script tag, and start using it in the JS.

I'm trying to do the same with VueJS in a component. I've got the template and style sections ready.

Unfortunately, I don't know how to add in a simple way a CDN to use inmediately in the script tag within the Vue component. I tried to do this but it is not working.

<template>
  <div class="index">
    <div class="container">
      <table id="table_dataset" class="display">
      </table>
    </div>
  </div>
  
</template>

<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></script>
<script>
  export default {
    name: 'Index',
    data() {
      return { 
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

Is there a way to add a CDN (without Webpack or NPM) to a VueJS component?

Answer

acdcjunior picture acdcjunior · Mar 17, 2018

Unfortunately, no, you can't add a <script> tag to a specific component via template.

In your case you have some options:

1: Use NPM

Propertly install the dependency using npm

  • Pros: proper usage of NPM and Webpack; scoped definition;
  • Cons: the script must be available as a NPM package.
  • Note: when available this is the recommended approach.
  • Steps:


2: Add <script> tag to index.html

Locate and a dd the <script> tag to your index.html

  • Pros: the <script> tag is clearly (and declaratively) added to the HTML source. The script will only be loaded once.
  • Cons: the script will be globally loaded.
  • Steps:
    • Just add the <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></script> to the end of the index.html file, preferably right before </body>.

3: Create the <script> tag programatically

The other alternative is to create the script tag programatically at the component, when the component is lodaded.

  • Pros: the code stays in the component only. Your external script will be loaded only when the component is loaded.
  • Cons: the script still will be globally available once it is loaded.
  • Steps/Code:

    <script>
      export default {
        name: 'Index',
        data() {
          return { 
          }
        },
        mounted() {
          if (document.getElementById('my-datatable')) return; // was already loaded
          var scriptTag = document.createElement("script");
          scriptTag.src = "https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js";
          scriptTag.id = "my-datatable";
          document.getElementsByTagName('head')[0].appendChild(scriptTag);
        }
      }
    </script>