[Vue] 原生HTML如果使用vue单文件组件(无webpack版本)

问题引出

我在写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,所以是当前路径了,也就是一个点,而不是两个点)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值