如何在没有NPM或Webpack的情况下将CDN包含到VueJS CLI中? [英] How to include a CDN to VueJS CLI without NPM or Webpack?
问题描述
我是VueJS和Webpack的新手。我用VueJS CLI创建了一个项目并试图使用它。我需要在我的代码中插入CDN。
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.
使用标准HTML,CSS& JS解决方案,我包括这样的CDN:
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>
如您所见,您可以添加带有HTML脚本标记的CDN脚本,并开始在JS中使用它。
As you can see, you can add a CDN script with the HTML script tag, and start using it in the JS.
我正在尝试在组件中对VueJS做同样的事情。我已准备好模板和样式部分。
I'm trying to do the same with VueJS in a component. I've got the template and style sections ready.
不幸的是,我不知道如何以简单的方式添加CDN以便在脚本标记内立即使用Vue组件。我试图这样做但它不起作用。
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>
有没有办法添加CDN(没有Webpack或NPM)到VueJS组件?
Is there a way to add a CDN (without Webpack or NPM) to a VueJS component?
推荐答案
不幸的是,不,你不能添加< script>
标记到特定组件通过模板。
Unfortunately, no, you can't add a <script>
tag to a specific component via template.
在您的情况下,您有一些选项:
In your case you have some options:
使用 npm $ c属性安装依赖项$ c>
- 优点: 正确使用NPM和Webpack;范围定义;
- 缺点: 脚本必须作为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:
-
对于您的情况,您可以办理
datatables
官方页面他们有NPM包。我可以这样使用:
For your case, you can check in
datatables
official page they do have a NPM package. I could be used like:
npm install --save datatables.net-dt
并在 .vue 文件中:
<script>
require( 'datatables.net-dt' )();
export default {
name: 'Index',
data() {
return {
}
}
}
</script>
找到< script>
标记到您的索引。 html
- 优点:
< script>
标记显然(并以声明方式)添加到HTML源代码中。该脚本只会加载一次。 - 缺点: 脚本将全局加载。
- 步骤:
- 只需添加
< script type =text / javascriptsrc =https:// cdn .datatables.net / v / dt / dt-1.10.16 / sl-1.2.5 / datatables.min.js>< / script>
到<$ c $的末尾c> index.html 文件,最好在< / body>
之前。
- 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 theindex.html
file, preferably right before</body>
.
另一种方法是在组件上以编程方式创建
脚本
标记已经寄了。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>
- Just add the
- 只需添加
这篇关于如何在没有NPM或Webpack的情况下将CDN包含到VueJS CLI中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!