vm.$emit( event, […args] )
觸發(fā)當(dāng)前實(shí)例上的事件构资。附加參數(shù)都會(huì)傳給監(jiān)聽(tīng)器回調(diào)蜓洪。
//父組件
<template>
<ratingselect @select-type="onSelectType"></ratingselect>
</template>
<script>
data () {
return {
selectType: 0,
},
methods: {
onSelectType (type) {
this.selectType = type
}
}
</script>
父組件使用@select-type="onSelectType"監(jiān)聽(tīng)由子組件vm.$emit觸發(fā)的事件辖众,通過(guò)onSelectType()接受從子組件傳遞過(guò)來(lái)的數(shù)據(jù)挂据,通知父組件數(shù)據(jù)改變了。
// 子組件
<template>
<div>
<span @click="select(0, $event)" :class="{'active': selectType===0}"></span>
<span @click="select(1, $event)" :class="{'active': selectType===1}"></span>
<span @click="select(2, $event)" :class="{'active': selectType===2}"></span>
</div>
</template>
<script>
data () {
return {
selectType: 0,
},
methods: {
select (type, event) {
this.selectType = type
this.$emit('select-type', type)
}
}
</script>