作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试遍历数组以呈现具有 type
值的组件。
<script>
import One from './One.svelte';
import Two from './Two.svelte';
import Three from './Three.svelte';
const contents = [
{type: 'One'},
{type: 'Two'},
{type: 'Three'},
{type: 'One'}
]
</script>
{#each contents as content}
<{content.type} />
{/each}
期望的输出:
<One />
<Two />
<Three />
<One />
执行此操作的最佳方法是什么?
最佳答案
使用 <svelte:component>
:
The
<svelte:component>
element renders a component dynamically, usingthe component constructor specified as the this property. When theproperty changes, the component is destroyed and recreated.
例如:
<script>
import One from './One.svelte';
import Two from './Two.svelte';
const contents = [
One,
Two
]
</script>
{#each contents as content}
<svelte:component this={content}/>
{/each}
https://svelte.dev/repl/e56e75ad9b584c44930fe96489a36e14?version=3.31.2
关于svelte - 如何在 Svelte 中动态渲染组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65656481/
我是一名优秀的程序员,十分优秀!