Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

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

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:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>False Merge</title>

    <!-- CDN -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.css"/>

    <!-- StyleSheets -->
    <link rel="stylesheet" href="public/stylesheets/index.css" />
</head>

<body>


    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></script>

    <script src="public/javascripts/index.js"></script>

</body>

</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...