最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sveltekit - No stopPropagation for Svelte 5? - Stack Overflow

programmeradmin1浏览0评论

I have tried searching all of the documentation for something about how to use stopPropagation for 'onclick' in Svelte 5, but can find nothing for it other than the legacy stuff. Is there any way to do this in Svelte 5?

I have tried something like this:

<div onclick|stopPropagation={someFunc}>
    <button onclick={otherFunc}></button>
</div>

But I get this error: "'onclick|stopPropagation' is not a valid attribute name".

How are you supposed to do this in Svelte 5?

I have tried searching all of the documentation for something about how to use stopPropagation for 'onclick' in Svelte 5, but can find nothing for it other than the legacy stuff. Is there any way to do this in Svelte 5?

I have tried something like this:

<div onclick|stopPropagation={someFunc}>
    <button onclick={otherFunc}></button>
</div>

But I get this error: "'onclick|stopPropagation' is not a valid attribute name".

How are you supposed to do this in Svelte 5?

Share Improve this question asked Nov 19, 2024 at 13:07 Lee ManLee Man 7262 gold badges8 silver badges32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

I would solve it like this:

<div onclick={(e) => { e.stopPropagation(); someFunc();}}>
    <button onclick={otherFunc}></button>
</div>

Modifiers are not supported for event properties. Either call the respective function in the handler directly, or use a higher order function to wrap the handler.

<script>
  function someFunc(e) {
    e.stopPropagation();
    // [other logic]
  }
</script>

<button onclick={someFunc}>...
<script>
  // extract to a module for reusability
  function stopPropagation(handler) {
    return e => {
      e.stopPropagation();
      handler(e);
    };
  }

  function someFunc() { /* ... */ }
</script>

<button onclick={stopPropagation(someFunc)}>...

(There is more info on this in the migration guide.)

发布评论

评论列表(0)

  1. 暂无评论