javascript - 如何在基本的 Vue html 文件中使用 Vue 组件 (.udm.js)?
问题描述
我构建了一个Vue component
与"build-bundle": "vue-cli-service build --target lib --name my-component ./src/myComponent.vue"
. 在dist
我的文件夹中:
- 演示.html
- 我的组件.common.js
- 我的组件.udm.js
- 我的组件.udm.min.js
现在,为了GitHub Page
发布,我正在构建一个页面来解释组件的细节和特性。我将使用一个/docs
文件夹作为根目录GitHub Page
(我不想使用gh-pages
分支)。
为此,我必须构建一个基本的Vue
应用程序,而不需要Vue CLI
,Webpack
或者这样。
index.html
看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
...
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="js/my-component.umd.js"></script>
...
</head>
<body>
...
<div id="app">..</div>
...
<script type="module">
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
components: {
myComponent: my-component
}
})
</script>
</body>
</html>
信息:我复制dist/my-component.umd.js
到该docs/js
文件夹以便拥有一个独立的文件夹。
如果我不使用 Vue 脚本中的组件(Vue 工作正常),页面运行良好。但是,当我使用该组件时,页面会显示此错误,该错误(index):199 Uncaught ReferenceError: vue is not defined
直接指向myComponent: my-component
.
有什么想法或建议吗?
解决方案
尝试将脚本放在应用代码之前的正文部分。否则,运行您的应用程序代码后可能会加载 Vue 库
<!DOCTYPE html>
<html lang="en">
<head>
...
...
</head>
<body>
...
<div id="app">..</div>
...
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="js/my-component.umd.js"></script>
<script type="module">
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
components: {
myComponent: my-component
}
})
</script>
</body>
</html>
推荐阅读
- powerbi - 如何根据 PowerBI 中的另一行对行求和?
- c++ - 如何连接 QT 和 MariaDB。未加载驱动程序
- regex - 正则表达式匹配除 0、1(11、12 等)以外的所有数字
- html - Angular Material Mat-菜单
- python - 使用无头时滚动的差异
- perl - perl拼接数组缺少条目
- elasticsearch - 无法通过带有 http://localhost:9200、9201、9203 的浏览器访问弹性搜索集群中 3 个节点中的 2 个(仅第一个)。这是正常的吗?为什么?
- python - 在 Jupyter Notebook 中键入 Markdown 和 LaTeX:2 错误
- python - 与单次读取并行将信息复制到少数数据库
- oracle-sqldeveloper - SQL Developer 代码完成仅当前架构