微信小程序阻止组件冒泡

/ 0评 / 0

一般情况下微信小程序事件冒泡:

bind : 不会阻止冒泡;

catch:阻止向上冒泡

需求背景:

<components bind:change="change" ></components>

如果上面组件父级有事件的话,回调change会触发父级事件的,尝试阻止父级冒泡,试了很多方法,解决方法如下:

<components bind:change="change" catchtap="stopPropagation"></components>

//js
change(e){
 console.log(e)
},
stopPropagation(){
  return
}

附:组件回调写法

componentsChange(e) {
   this.triggerEvent('change', {label: e.detail});
}
//回调到组件的e.detail.label
如有错误,欢迎留言纠正!

发表评论

您的电子邮箱地址不会被公开。