使用@tap.stop阻止事件繼續(xù)傳播
@tap.stop阻止事件繼續(xù)傳播
在uni-app開發(fā)當中,難免會遇到外層view嵌套內層view,又同時都含有點擊事件,這樣就會起沖突。
為了防止事件的繼續(xù)傳播我們就會用到事件修飾符.stop
先看代碼:
<template> ?? ?<view class="wai" @tap="waiTab"> ?? ??? ?<h5>外面</h5> ?? ??? ?<view class="nei" @tap="neiTab"> ?? ??? ??? ?<h5>內部</h5> ?? ??? ?</view> ?? ?</view> </template>
<script> ?? ?export default { ?? ??? ?data() { ?? ??? ??? ?return { ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ?}, ?? ??? ?methods: { ?? ??? ??? ?waiTab(){ ?? ??? ??? ??? ?console.log("點擊了外邊") ?? ??? ??? ?}, ?? ??? ??? ?neiTab(){ ?? ??? ??? ??? ?console.log("點擊了內部") ?? ??? ??? ?} ?? ??? ?} ?? ?} </script>
<style> ?? ?.wai{ ?? ??? ?width: 100%; ?? ??? ?height: 100px; ?? ??? ?display: flex; ?? ??? ?justify-content: center; ?? ??? ?background-color: #0000FF; ?? ?} ?? ?.nei{ ?? ??? ?display: flex; ?? ??? ?justify-content: center; ?? ??? ?align-items: center; ?? ??? ?width: 50px; ?? ??? ?height: 50px; ?? ??? ?background-color: #00CE47; ?? ?} </style>
效果是這樣的:
當我們點擊外部時:
當我們點擊內部時:
解決方法:只需在@tap后面加.stop就可以阻止事件繼續(xù)傳播
<view class="wai" @tap.stop="waiTab"> <h5>外面</h5> <view class="nei" @tap.stop="neiTab"> <h5>內部</h5> </view> </view>
uniapp+uview @tap.stop="stop"組織冒泡失效bug
阻止事件冒泡時,直接在需要使用的方法上加.stop無效
需要在外層加一層標簽 <view @tap.stop=“stop”>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue element-ui el-cascader級聯選擇器數據回顯的兩種實現方法
這篇文章主要介紹了vue element-ui el-cascader級聯選擇器數據回顯的兩種實現方法,具有很好的參考價值,希望對大家有所幫助。2023-07-07一步步教你搭建VUE+VScode+elementUI開發(fā)環(huán)境
這篇文章主要給大家介紹了關于搭建VUE+VScode+elementUI開發(fā)環(huán)境的相關資料,近期被配置環(huán)境的事情弄得整個人都要炸了,現在整理如下,希望有相同需求的朋友可以不用走彎路,需要的朋友可以參考下2023-07-07解決在vue+webpack開發(fā)中出現兩個或多個菜單公用一個組件問題
這篇文章主要介紹了在vue+webpack實際開發(fā)中出現兩個或多個菜單公用一個組件的解決方案,需要的朋友可以參考下2017-11-11