与子组件交互时,父组件的事件触发。下面是我的代码示例:
<!-- Parent Component -->
<div class="parent" on:click={toggle}>
{#if expand}
<Child />
{/if}
</div>
<!-- Child Component -->
<div class="child" on:mousedown|stopPropagation={execute}>
text
</div>
我尝试在函数调用中使用
stopPropagation
和 preventDefault
。
这些是不同的事件,停止一种类型不会停止另一种,即使它们都与单击操作相关。如果您在家长上听
click
,则必须停止在孩子上click
。
另外,使用按钮。