Vue组件注册

1.全局组件注册语法

2.1 全局组件注册语法
    参数1:组件名称;  
    参数2:组件对象(data(组件内部需要的数据),template(模板内容))
    Vue.component('button-counter', {
        data: function(){
            return {
                count: 0
            }
        },
        template: '<button @click="handle">点击了{{count}}次</button>',
        methods: {
            handle: function(){
                this.count +=2;
            }
        }
    });
    

2.组件使用 , 可重复使用

<div id="app">
        <button-counter></button-counter>
    </div>
    
    <div id="app">
        <button-counter></button-counter>
        <button-counter></button-counter>
        <button-counter></button-counter>
    </div>

 

<!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>
	</body>
	
	<script type="text/javascript" src="../js/vue.js"></script>
	<script type="text/javascript" >
	
	/*
	组件注册
	2.1 全局组件注册语法
	参数1:组件名称;  
	参数2:组件对象(data(组件内部需要的数据),template(模板内容))
	Vue.component('button-counter', {
		data: function(){
			return {
				count: 0
			}
		},
		template: '<button @click="handle">点击了{{count}}次</button>',
		methods: {
			handle: function(){
				this.count +=2;
			}
		}
	});
	
	组件使用 , 可重复使用
	<div id="app">
		<button-counter></button-counter>
	</div>
	
	<div id="app">
		<button-counter></button-counter>
		<button-counter></button-counter>
		<button-counter></button-counter>
	</div>

	 */
	Vue.component('button-counter', {
		data: function(){
			return {
				count: 0
			}
		},
		template: '<button @click="handle">点击了{{count}}次</button>',
		methods: {
			handle: function(){
				this.count +=2;
			}
		}
	});
		var vm = new Vue({
			el:'#app',
			data:{
				 msg: "hello",
			},
			methods: {
				handle: function (event) {
					
				}
			}
		});
	</script>
</html>

3.组件注册注意事项

        1. data必须是一个函数
        2. 组件模板内容必须是单个根元素
        3. 组件模板内容可以是模板字符串(使用反引号)
            模板字符串需要浏览器提供支持(ES6语法)
            template:   

                 `<div>
                    <button @click="handle">点击了{{count}}次</button>
                    <button>测试</button>
                </div>`

        4.组件的命名方式
            短横线方式
            Vue.component('button-counter', {})
            驼峰方式
            如果使用驼峰式命名组件,那么在使用组件的时候,
            只能在字符串模板中用驼峰的方式使用组件,但是
            在普通的标签模板中,必须使用短横线的方式使用
            Vue.component('helloWorld', {})    

 

<!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>
	</body>
	
	<script type="text/javascript" src="../js/vue.js"></script>
	<script type="text/javascript" >
	
	/*
	2.3 组件注册注意实现
		1. data必须是一个函数
		2. 组件模板内容必须是单个根元素
		3. 组件模板内容可以是模板字符串
			模板字符串需要浏览器提供支持(ES6语法)
			template: `
				<div>
					<button @click="handle">点击了{{count}}次</button>
					<button>测试</button>
				</div>
			`	
	 */
	Vue.component('button-counter', {
		data: function(){
			return {
				count: 0
			}
		},
		template: `
			<div>
				<button @click="handle">点击了{{count}}次</button>
				<button>测试</button>
			</div>
		`,
		methods: {
			handle: function(){
				this.count +=2;
			}
		}
	});
		var vm = new Vue({
			el:'#app',
			data:{
				 msg: "hello",
			},
			methods: {
				handle: function (event) {
					
				}
			}
		});
	</script>
</html>
<!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>
			<hello-world></hello-world>
			
		</div>
	</body>
	
	<script type="text/javascript" src="../js/vue.js"></script>
	<script type="text/javascript" >
	
	/*
	2.3 组件注册注意事项
		1. data必须是一个函数
		2. 组件模板内容必须是单个根元素
		3. 组件模板内容可以是模板字符串
			模板字符串需要浏览器提供支持(ES6语法)
			template: 
				`<div>
					<button @click="handle">点击了{{count}}次</button>
					<button>测试</button>
				</div>`
		4.组件的命名方式
			短横线方式
			Vue.component('button-counter', {})
			驼峰方式
			如果使用驼峰式命名组件,那么在使用组件的时候,
			只能在字符串模板中用驼峰的方式使用组件,但是
			在普通的标签模板中,必须使用短横线的方式使用
			Vue.component('helloWorld', {})	
			
	 */
	Vue.component('helloWorld', {
		data: function(){
			return {
				msg: 'helloWord'
			}
		},
		template: '<div>{{msg}}</div>'
		
	})
	
	Vue.component('button-counter', {
		data: function(){
			return {
				count: 0
			}
		},
		template: `
			<div>
				<button @click="handle">点击了{{count}}次</button>
				<button>测试</button>
				<helloWorld></helloWorld>
			</div>
		`,
		methods: {
			handle: function(){
				this.count +=2;
			}
		}
	});
		var vm = new Vue({
			el:'#app',
			data:{
				 msg: "hello",
			},
			methods: {
				handle: function (event) {
					
				}
			}
		});
	</script>
</html>

4. 注册局部组件

局部组件只能在注册他的父组件中使用
        1.定义一个组件对象
        var HelloJack = {
            data: function(){
                return {
                    msg: '窗外的麻雀'
                }
            },
            template: '<div>{{msg}}</div>'
        };
        2.在Vue对象中注册
        var vm = new Vue({
            el:'#app',
            components: {
                'hello-jack': HelloJack,
                'hello-marry': HelloMarry,
                'hello-tom': HelloTom
            }        
        });

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="app">
			<hello-jack></hello-jack>
			<hello-marry></hello-marry>
			<hello-tom></hello-tom>
		</div>
	</body>
	
	<script type="text/javascript" src="../js/vue.js"></script>
	<script type="text/javascript" >
	
	/*
		局部组件注册
		局部组件只能在注册他的父组件中使用
		1.定义一个组件对象
		var HelloJack = {
			data: function(){
				return {
					msg: '窗外的麻雀'
				}
			},
			template: '<div>{{msg}}</div>'
		};
		2.在Vue对象中注册
		var vm = new Vue({
			el:'#app',
			components: {
				'hello-jack': HelloJack,
				'hello-marry': HelloMarry,
				'hello-tom': HelloTom
			}		
		});
	 */
	var HelloJack = {
		data: function(){
			return {
				msg: '窗外的麻雀'
			}
		},
		template: '<div>{{msg}}</div>'
	};
	var HelloMarry = {
		data: function(){
			return {
				msg: '在电线杆上多嘴'
			}
		},
		template: '<div>{{msg}}</div>'
	};
	var HelloTom = {
		data: function(){
			return {
				msg: '你说着一句'
			}
		},
		template: '<div>{{msg}}</div>'
	};
		var vm = new Vue({
			el:'#app',
			data:{
				 
			},
			components: {
				'hello-jack': HelloJack,
				'hello-marry': HelloMarry,
				'hello-tom': HelloTom
			},
			methods: {
				handle: function (event) {
					
				}
			}
		});
	</script>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值