在做项目的时候遇到了switch和祖父元素事件冒泡的问题。刚开始想的解决办法是在change里面加e.preventDefault来阻止,然鹅不生效。就抱着试一试的方法在switch的父元素加上stop试试,竟然成功了。
<view v-for="(item, index) in deviceList" :key="index" @tap="airPanel(item, deviceType)" > <view class="device-switch" @tap.stop.prevent> <switch @change="deviceSwitch($event, index, deviceType)" :checked="item.isOpen == 1" ></switch> </view> </view>
在switch的父标签加@tap.stop.prevent阻止事件冒泡。