将可执行函数引用 to/from 传递给 child Svelte 组件?

Passing an executable function reference to/from a child Svelte component?

你好。我怎样才能成功完成这个循环?

  1. 将函数名称从 parent 组件传递到 child 组件
  2. 在事件(例如单击)上具有 child 组件 return 函数名称
  3. 执行 parent 组件中的函数名(现在是字符串)

需要这个的一个例子是在应用程序 Header 中使用类似 DropdownMenu 组件的东西 – 其中 header 有一个注销 link/handler 传递给菜单混合其他 href links/actions.

这是我目前正在使用的方法(使用 window[functionName])的 REPL 示例,该方法尚未奏效 –

https://svelte.dev/repl/280626d841d84dd08dd944ec249e4e6f?version=3.38.2

感谢您的帮助。

看起来这个问题的解决方案就是不将父组件中的函数作为字符串传递。

将它们作为文字引用传递并在子组件中一般地调用它们是可行的。

已更新工作版本的 REPL -

https://svelte.dev/repl/5b8bb118e0aa47d38d974cbc1fd23453?version=3.38.2