【Vue】-(9) 组件-1

组件这部分东西有点多,记录一下

//传统注册方法
//定义一个组件
var MyComponent = Vue.extend({
	template:'<div>A custom component</div>'
});
//注册
Vue.component('my-component',MyComponent);


//简单注册方法(child只能在parent内使用)
//一站式注册。。
Vue.component('my-component1',{
	template:'<div>A simple component!</div>'
});

///
//局部注册:如下,child注册在parent里面作为my-child组件,只能在parent里面使用
var child =Vue.extend({
	template:"<div>child</div>"
})
var parent=Vue.extend({
	template:"<my-child></my-child>",
	components:{
		'my-child':child
	} 
})
Vue.component('my-parent',parent)








//组件选项问题
//在Vue.extend中,如果使用data和el,如果如下
var data={a:1}
var myComponenet=Vue.extend({
	data:data
})
//这样做会将myComponenet的所有实例共享同一个data
//所以《当使用data和el的时候,需要这样
var MyComponenet = Vue.extend({
	data:function(){
		return {a:1}
	}
})


//Props
Vue.component('child',{
	//声明props
	//注意:形如myPro这种,在页面要使用my-pro
	props:['msg'],
	template:'<span>{{msg}}</span>'
});

//Vue.component('child', {
 camelCase in JavaScript
//props: ['myMessage'],
//template: '<span>{{ myMessage }}</span>'
//});

//创建根实例
new Vue({
	el:'#example',
	data:{
		text:'text'
	}
});

html:

<div id="example">
			<my-parent></my-parent>
			 Props:<br />
			 <input v-model="text" /><!--因为这是双向绑定,所以text改变 -->
	 		<child :msg="text"></child><!-- 这是单向绑定,被Input改变 -->
	</div> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值