_layout.svelte 内的插槽属性未传递属性

2024-04-01

我正在开发一个 Sapper 项目,我想在加载到插槽中之前将一些异步数据加载到布局中。我发现在 _layout.svelte 文件中,我无法将道具传递到插槽。

//_layout.svelte
<slot foo={"hello"}></slot>

//index.svelte
<script>
  export let foo;
  alert(foo); // returns undefined
</script>

有人遇到过这个吗?我想我可以通过在每个槽/子页面上加载我需要的所有数据来解决这个问题。我能够设置插槽道具的唯一方法是手动访问它。

$$props.$$scope.ctx.level1.props.foo = "hello"

像这样传递数据似乎不起作用。您可以使用context https://svelte.dev/docs#setContext though:

// in _layout.svelte
import {setContext} from 'svelte';
setContext('foo', foo);
// in index.svelte
import {getContext} from 'svelte';
const foo = getContext('foo');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

_layout.svelte 内的插槽属性未传递属性 的相关文章

  • CoreBluetooth 应用程序在后台到底可以做什么?

    主题已经说明了一切 真的 就其存在而言 文档表明针对 iOS 设备上运行的 CoreBluetooth 框架编写的应用程序可以将 bluetooth central 添加到其后台权限列表中 从而在不活动时处理某种蓝牙事件 但exact事件是

随机推荐