vue 如何根据props动态为事件添加stop修饰符?
在Vue中,可以使用v-bind指令动态绑定事件的修饰符。通过props传递一个布尔值,然后在组件中使用v-bind来绑定事件时,根据该布尔值来动态添加.stop修饰符。
例如,假设父组件传递了一个名为stopEvent的props,值为true,子组件中的事件绑定可以这样写:
<template>
<div @click="clickHandler"></div>
</template>
<script>
export default {
props: {
stopEvent: {
type: Boolean,
default: false
}
},
methods: {
clickHandler(event) {
if (this.stopEvent) {
event.stopPropagation();
}
// do something
}
}
}
</script>
在事件处理函数中,根据stopEvent的值来判断是否需要添加.stop修饰符,如果为true则调用event.stopPropagation()方法来阻止事件冒泡。
原文地址: https://www.cveoy.top/t/topic/bhaF 著作权归作者所有。请勿转载和采集!