8.vue2-组件component

Author: vanessa
Date: 2018/06/19

1.组件

<my-div></my-div>
Vue.component('my-div',{
		template:'<button>按钮</button>',
	})

2.父组件给子组件传递数据props

<my-div title="this is a element"></my-div>
Vue.component('my-div',{
		props:['title'],
		template:'<p>{{title}}</p>',
	})

3.向父级发送消息emit

使用emit提交事件,emit的第一个参数相当于父级需要@后面的值,第二个参数相当于在父组件中通过$event访问

<my-div :style={fontSize:txtFontSize+'em'} title="this is a element" @large-font="txtFontSize+=$event"></my-div></div>
Vue.component('my-div',{
		props:['title'],
		template: `
		    <div>
		       <button @click="$emit('large-font',0.1)">large text</button>
		       <p>{{title}}</p>
		    </div>`,
	})
    new Vue({
		el:'#box',
		data:{
			txtFontSize:1,
        }
    })

4.组件中使用v-model

Vue.component('custom-input',{
		props:['searchText'],
		template:`<div>
			<input type="text" v-bind:value="searchText" @input="$emit('input', $event.target.value)"/></div>
		`
	});
<custom-input v-model="searchText"></custom-input>

ps:searchText如果定义中data中,一开始并没有绑定值,为什么?
答:组件中props中必须是”value“,template中必须是:v-bind:value=’value‘,不能是传递的searchText

5.插槽solt,父向子传递内容,父组件中间的内容被插到子组件solt中

 Vue.component('alert-box', {
  template: `
    <div class="demo-alert-box">
      <strong>Error!</strong>
      <slot></slot>
    </div>
  `
})
<alert-box>
  Something bad happened.
</alert-box>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值