017_Vue组件

1. 全局组件

1.1. 全局组件注册语法

Vue.component(组件名称, {
	data: 组件数据,
	template: 组件模板内容,
	methods: {}
})

1.2. 组件使用, 组件可以重用

<div id="app">
	<组件名称></组件名称>
    <组件名称></组件名称>
    <组件名称></组件名称>
</div>

1.3. 代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>全局组件</title>
	</head>
	<body>
		<div id="app">
			<button-counter></button-counter>
		    <button-counter></button-counter>
		    <button-counter></button-counter>
		</div>
		
		<script type="text/javascript" src="vue.min.js"></script>
		<script type="text/javascript">
			// 自定义全局组件
			Vue.component("button-counter", {
				data: function(){
					return {
						count: 0
					}
				},
				template: "<button @click='handle'>点击了{{count}}次</button>",
				methods: {
					handle: function(){
						this.count += 1;
					}
				}
			});

			var vm = new Vue({
				el: "#app",
				data: {
					
				}
			});
		</script>
	</body>
</html>

1.4. 效果图

1.5. 组件的注意事项

1.5.1. data必须是一个函数。

1.5.2. 组件模板内容必须是单个根元素。

2. 模板字符串

2.1. 组件模板内容可以是模板字符串, 模板字符串需要浏览器提供支持(ES6语法), 解决模板内容较长问题。

 

2.2. 代码 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>组件模板字符串</title>
	</head>
	<body>
		<div id="app">
			<button-counter></button-counter>
		</div>
		
		<script type="text/javascript" src="vue.min.js"></script>
		<script type="text/javascript">
			Vue.component("button-counter", {
				data: function() {
					return {
						count: 0
					}
				},
				template: `
					<div>
						<button @click="handlePlus">加加{{count}}</button>
						<button @click="handleMinus">减减{{count}}</button>
					</div>
				`,
				methods: {
					handlePlus: function(){
						++this.count;
					},
					handleMinus: function(){
						--this.count;
					}
				}
			});

			var vm = new Vue({
				el: "#app",
				data: {
					
				}
			});
		</script>
	</body>
</html>

2.3. 效果图

3. 组件命名方式

3.1. 短横线方式

Vue.component('my-component', {})

3.2. 驼峰方式(尽可能不用)

Vue.component('MyComponent', {})

3.3. 如果使用驼峰式命名组件, 那么在使用组件的时候, 只能在字符串模板中用驼峰的方式使用组件, 但是在普通的标签模板中, 必须使用短横线的方式使用组件。

3.4. 代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>组件命名</title>
	</head>
	<body>
		<div id="app">
			<hello-world></hello-world>
			<button-counter></button-counter>
		</div>
		
		<script type="text/javascript" src="vue.min.js"></script>
		<script type="text/javascript">
			// 如果使用驼峰式命名组件, 那么在使用组件的时候, 只能在字符串模板中用驼峰的方式使用组件, 但是在普通的标签模板中, 必须使用短横线的方式使用组件。
			Vue.component("HelloWorld", {
				data: function() {
					return {
						msg: "HelloWorld"
					}
				},
				template: "<div>{{msg}}</div>"
			});

			Vue.component("button-counter", {
				data: function() {
					return {
						count: 0
					}
				},
				template: `
					<div>
						<button @click="handle">点击了{{count}}次</button>
						<HelloWorld></HelloWorld>
					</div>
				`,
				methods: {
					handle: function(){
						++this.count;
					}
				}
			});

			var vm = new Vue({
				el: "#app",
				data: {

				}
			});
		</script>
	</body>
</html>

3.5. 效果图

4. 局部组件

4.1. 局部组件使用方式

 

4.2. 代码 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>局部组件</title>
	</head>
	<body>
		<div id="app">
			<hello-world></hello-world>
			<button-counter></button-counter>
		</div>
		
		<script type="text/javascript" src="vue.min.js"></script>
		<script type="text/javascript">
			var helloWorld = {
				data: function() {
					return {
						msg: "HelloWorld"
					}
				},
				template: "<div>{{msg}}</div>"
			};

			var buttonCounter = {
				data: function(){
					return {
						count: 0
					}
				},
				template: "<button @click='handle'>点击了{{count}}次</button>",
				methods: {
					handle: function(){
						this.count += 1;
					}
				}
			}

			var vm = new Vue({
				el: "#app",
      			components: {
        			'hello-world': helloWorld,
        			'button-counter': buttonCounter
      			}
			});
		</script>
	</body>
</html>

4.3. 效果图

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值