Vue组件

组件:可以扩展html,封装重复的代码
使用:

1.全局组件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="../js/vue.js">
			
		</script>
	</head>
	<body>
		<div id="app">
			
			<!-- 3.2 使用组件 -->
			<Person></Person>
			
		</div>
		
		<div id="a">
			<Person></Person>
		</div>
		
		<script type="text/javascript">
			// 全局组件:作用范围是多个Vue对象使用,可以再所有的渲染区使用
			<!--3.1 创建全局组件 -- 1.是组件名,2.是组件内容 -->
			// Vue.component(1,2)
			Vue.component('Person',{
				// template:组件具体要啥内容
				template:"<h1>姓名:jack,年龄:20</h1>"
			})
			
			new Vue({	
				el:"#app",
				components:{
					starter:{//组件名
						template:'<h1>哈哈</h1>'
					}
				}
			})
			
			
			new Vue({
				el:"#a",
				
			})
		</script>
	</body>
</html>

2.局部组件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="../js/vue.js">
			
		</script>
	</head>
	<body>
		<div id="app">
			<starter></starter>
			<Student></Student>
		</div>
		
		<div id="a">
			<Student></Student>
		</div>
		<script type="text/javascript">
			Vue.component('Student',{
				template:"<h2>学号:2018124118</h2>"
			})
			new Vue({
				el:"#app",
				components:{
					starter:{
						template:"<h4>jack</h4>"
					}
				}
			})
			new Vue({
				el:"#a"
			})
		</script>
	</body>
</html>

整合案例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="../js/vue.js">
			
		</script>
	</head>
	<body>
		<div id="app">
			<name></name>
			<starter></starter>
			<happy></happy>
			<age></age>
			<stu></stu>
		</div>
		<script type="text/javascript">
			Vue.component('name',{
				template:'<h1>你好啊~</h1>'
			})
			Vue.component('age',{
				template:'<ol><li>我是元素1</li><li>我是元素2</li></ol>'
			})
			Vue.component('stu',{
				template:'<button style="background-color:cadetblue;">点击</button>'
			})
			new Vue({
				el:"#app",
				components:{
					starter:{
						template:'<h2>I AM FINE !!</h2>'
					},
					happy:{
						template:'<button style="background-color: aquamarine;">按钮</button>'
					}
				}
			})
		</script>
	</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值