将 html 添加到字符串 svelte

0 html javascript svelte

嘿,我有几串文字

let text = [
    "<strong>Voiijer</strong>" +
      "is a global community of explorers of all ages who create and share expeditions",
    "Everyone is an explorer, now everyone can explore. Voiijer is for everyone, from students to scientists.",
    "Voiijer is for the naturally curious. <br />To tell the story of the world.",
  ];
Run Code Online (Sandbox Code Playgroud)

我这样显示

<p>{text[index]}</p>
Run Code Online (Sandbox Code Playgroud)

索引是 carousal 元素的索引,但 html 元素仅显示为文本。有人知道如何解决这个问题吗?

Bal*_*ong 7

你可以用以下方式显示它{@html text[index]}

但是,根据文档

在插入 DOM 之前,Svelte 不会对 {@html ...} 内的表达式执行任何清理。换句话说,如果您使用此功能,手动转义来自您不信任的来源的 HTML 至关重要,否则您的用户将面临 XSS 攻击的风险。

您应该确保您自己的绳子是安全的。