阻止事件冒泡,大家应该都知道,就不多说了,先上个场景,如下图
想当然的,给change事件加 .stop 来阻止冒泡
<div>
<el-switch
v-model="currentValue"
active-value="1"
inactive-value="2"
active-color="#0052D9"
inactive-color="#C5C5C5"
@change.stop="switchChange">
</el-switch>
</div>
但会有如下的报错
vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler: "TypeError: $event.stopPropagation is not a function"vue.runtime.esm.js?2b0e:1888 TypeError: $event.stopPropagation is not a function
很显然,.stop 只能给 click 事件加,那怎么办呢?
给外层div盒子加 click 事件,完美!
<div @click.stop>
<el-switch
v-model="currentValue"
active-value="1"
inactive-value="2"
active-color="#0052D9"
inactive-color="#C5C5C5"
@change="switchChange">
</el-switch>
</div>