问题引出
我在写html的时候,想在当前html页面引入外部组件,方便后期维护;于是就想到vue,但是又不想使用vue脚手架,只是单纯想使用vue组件,并且是创建.vue结尾的文件,而不是用``括起来的组件
直接上代码
项目整体目录图
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
<script src="https://unpkg.com/http-vue-loader"></script>
<script src="js/index.js"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div>
</body>
</html>
index.js
$(document).ready(function() {
// 使用httpVueLoader
Vue.use(httpVueLoader);
new Vue({
el: '#app',
data: function() {
return {
visible: false
}
},
components: {
// 将组建加入组建库
'my-component': 'url:./component/my-component.vue'
}
})
});
my-component.vue
$(document).ready(function() {
// 使用httpVueLoader
Vue.use(httpVueLoader);
new Vue({
el: '#app',
data: function() {
return {
visible: false
}
},
components: {
// 将组建加入组建库
'my-component': 'url:./component/my-component.vue'
}
})
});
index.html运行结果
注意事项
在index.js里面的路径’my-component’: ‘url:./component/my-component.vue’;千万不能写错了.
我就是错写成’my-component’: ‘url:…/component/my-component.vue’(多了个点,导致一直运行不出来,因为在index.html引入的index.js,所以是当前路径了,也就是一个点,而不是两个点)