我在页面上有几个嵌套的组件与具有@click.native实现的父组件。因此,当我单击子组件(位于父组件内部)所占据的区域时,两个单击操作都会执行(父组件和所有嵌套子组件),例如
<products>
<product-details>
<slide-show>
<media-manager>
<modal-dialog>
<product-details>
<slide-show>
<media-manager>
<modal-dialog>
</products>所以我有一个多个产品的列表,当我点击属于modal的“画布”时,我也会在modal dialog所属的product-details上触发@click.native。如果有像@click.native.stop="code"这样的东西会很好,这可能吗?
现在我必须这样做:
@click.native="clickHandler"
and then
methods: {
clickHandler(e) {
e.stopPropagation();
console.log(e);
}代码
<template>
<div class="media-manager">
<div v-if="!getMedia">
<h1>When you're ready please upload a new image</h1>
<a href="#"
class="btn btn--diagonal btn--orange"
@click="upload=true">Upload Here</a>
</div>
<img :src="getMedia.media_url"
@click="upload=true"
v-if="getMedia">
<br>
<a class="arrow-btn"
@click="upload=true"
v-if="getMedia">Add more images</a>
<!-- use the modal component, pass in the prop -->
<ModalDialog
v-if="upload"
@click.native="clickHandler"
@close="upload=false">
<h3 slot="header">Upload Images</h3>
<p slot="body">Hello World</p>
</ModalDialog>
</div>
</template>
<script>
import ModalDialog from '@/components/common/ModalDialog';
export default {
components: {
ModalDialog,
},
props: {
files: {
default: () => [],
type: Array,
},
},
data() {
return {
upload: false,
}
},
computed: {
/**
* Obtain single image from the media array
*/
getMedia() {
const [
media,
] = this.files;
return media;
},
},
methods: {
clickHandler(e) {
e.stopPropagation();
console.log(e);
}
}
};
</script>
<style lang="scss" scoped>
.media-manager img {
max-width: 100%;
height: auto;
}
a {
cursor: pointer;
}
</style>发布于 2018-03-21 00:37:21
你看过手册了吗?https://vuejs.org/v2/guide/events.html
有@click.stop=""或@click.stop.prevent=""
所以你不需要用这个
methods: {
clickHandler(e) {
e.stopPropagation();
console.log(e);
}
}发布于 2019-07-10 09:57:59
在Vue中,可以链接修饰符。所以,你可以像这样自由地使用修饰符:
@click.native.prevent或@click.stop.prevent
<my-component @click.native.prevent="doSomething"></my-component>检查events
发布于 2019-04-23 00:20:35
我也有同样的问题。我使用以下命令修复了这个问题:
<MyComponent @click.native.prevent="myFunction(params)" />https://stackoverflow.com/questions/49041779
复制相似问题