不像 onMount
没有beforeMount
SvelteJS 中的生命周期事件。那么,我们如何在挂载之前获取页面所依赖的数据呢? onMount
fetches 会产生故障。可以说我可以将依赖 DOM 包装在 if 条件中。但我认为这不是正确的解决方案。很像在 Sapper 中有一个 preload
可以在安装前加载页面相关数据的功能。 SvelteJS 中这种(Sapper 的 preload
)行为的替代方法是什么?
最佳答案
你可以把fetch
代码正下方 <script>
标签
A
<script>
block contains JavaScript that runs when a component instance is created.
还有
context="module"
属性与 <script>
标签。它runs once when the module first evaluates, rather than for each component instance
见 the official docs
关于events - 如何在 Svelte 组件安装之前获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58604895/