[英]How to use a Vue Component (.udm.js) in a basic Vue html file?
I built a Vue component
that was packaged with "build-bundle": "vue-cli-service build --target lib --name my-component ./src/myComponent.vue"
. 我构建了一个
Vue component
,该Vue component
与"build-bundle": "vue-cli-service build --target lib --name my-component ./src/myComponent.vue"
打包在一起"build-bundle": "vue-cli-service build --target lib --name my-component ./src/myComponent.vue"
。 In the dist
folder I have: 在
dist
文件夹中,我有:
Now, for the GitHub Page
publication, I'am building a page explaining the details and the features of the component. 现在,对于
GitHub Page
出版物,我正在构建一个页面,解释该组件的详细信息和功能。 I'll use a /docs
folder as root for the GitHub Page
(I don't want to use a gh-pages
branch). 我将
/docs
文件夹用作GitHub Page
根目录(我不想使用gh-pages
分支)。
For this, I have to build a basic Vue
App without Vue CLI
, Webpack
or so. 为此,我必须构建一个没有
Vue CLI
或Webpack
的基本Vue
应用程序。
The index.html
looks like this: 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>
Info: I copied dist/my-component.umd.js
to the docs/js
folder in order to have a standalone folder. 信息:我将
dist/my-component.umd.js
复制到docs/js
文件夹中,以获得一个独立的文件夹。
The page works well if I do not use the component in the Vue script (Vue works properly). 如果我不使用Vue脚本中的组件,则该页面效果很好(Vue正常工作)。 But, when I use the component, the page shows this error
(index):199 Uncaught ReferenceError: vue is not defined
which is pointing directly to myComponent: my-component
. 但是,当我使用组件时,页面显示此错误
(index):199 Uncaught ReferenceError: vue is not defined
,它直接指向myComponent: my-component
。
Any idea or suggestion? 有什么想法或建议吗?
Try putting the scripts in the body section before your app code. 尝试将脚本放在应用程序代码之前的主体部分中。 Otherwise, Vue library could be loaded after running your app code
否则,可以在运行您的应用程序代码后加载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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.