如何在没有 NPM 或 Webpack 的情况下将 CDN 包含到 VueJS CLI? [英] How to include a CDN to VueJS CLI without NPM or Webpack?
问题描述
我是 VueJS 和 Webpack 的新手.我已经使用 VueJS CLI 创建了一个项目并尝试使用它.我需要在我的代码中插入 CDN.
使用标准 HTML、CSS 和JS 解决方案,我会包括这样的 CDN:
<头><meta charset="utf-8"/><title>虚假合并</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"/><!-- 样式表--><link rel="stylesheet" href="public/stylesheets/index.css"/>头部><身体><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 src="public/javascripts/index.js"></script></html>
如您所见,您可以添加带有 HTML script 标签的 CDN 脚本,并开始在 JS 中使用它.
我正在尝试对组件中的 VueJS 做同样的事情.我已经准备好了模板和样式部分.
不幸的是,我不知道如何以简单的方式添加 CDN 以便在 Vue 组件内的脚本标签中立即使用.我试图这样做,但它不起作用.
<div class="index"><div class="容器"><table id="table_dataset" class="display">
模板><script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></脚本><脚本>导出默认{name: '索引',数据() {返回 {}}}<!-- 添加scoped"属性以将 CSS 限制为仅适用于该组件 --><样式范围></style>
有没有办法将 CDN(没有 Webpack 或 NPM)添加到 VueJS 组件中?
不幸的是,不,您不能将 标签添加到特定组件通过模板强>.
就您而言,您有一些选择:
1:使用 NPM
使用 npm
- 优点:正确使用 NPM 和 Webpack;范围定义;
- 缺点:脚本必须作为 NPM 包提供.
- 注意:这是推荐方法.
步骤:
对于您的情况,您可以查看
datatables
官方页面,他们确实有NPM 包.我可以像这样使用:npm install --save datatables.net-dt
在您的 .vue 文件中:
2:在index.html
中添加
标签找到 标记并将其添加到您的
index.html
- 优点:
标记已明确(并以声明方式)添加到 HTML 源代码中.该脚本只会加载一次.
- 缺点:脚本将被全局加载.
- 步骤:
- 只需添加
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></script>
到index.html
文件的末尾,最好在</body>
之前.莉>
- 只需添加
3:以编程方式创建
标签
另一种方法是在加载组件时以编程方式在组件上创建 script
标记.
- 优点:代码仅保留在组件中.只有在加载组件时才会加载您的外部脚本.
- 缺点:该脚本在加载后仍将全局可用.
步骤/代码:
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>
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?
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:
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
And in your .vue file:
<script> require( 'datatables.net-dt' )(); export default { name: 'Index', data() { return { } } } </script>
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 theindex.html
file, preferably right before</body>
.
- Just add the
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>
这篇关于如何在没有 NPM 或 Webpack 的情况下将 CDN 包含到 VueJS CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!