gpt4 book ai didi

javascript - 如何在基本 Vue html 文件中使用 Vue 组件 (.udm.js)?

转载 作者:行者123 更新时间:2023-12-02 23:04:07 25 4
gpt4 key购买 nike

我构建了一个 Vue 组件,它是用 "build-bundle": "vue-cli-service build --target lib --name my-component ./src/myComponent 打包的.vue”。在 dist 文件夹中,我有:

  • 演示.html
  • 我的组件.common.js
  • 我的组件.udm.js
  • my-component.udm.min.js

现在,对于 GitHub Page 出版物,我正在构建一个页面来解释该组件的详细信息和功能。我将使用 /docs 文件夹作为 GitHub Page 的根目录(我不想使用 gh-pages 分支)。

为此,我必须构建一个基本的 Vue 应用程序,而无需 Vue CLIWebpack 等。

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>

关于javascript - 如何在基本 Vue html 文件中使用 Vue 组件 (.udm.js)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57661947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com