将特殊字符从变量传递给按钮

Pass special charactor to button from variable

var text = '‹';
    
<button>{text}</button>

这不会显示 ,而是 &lsaquo; 作为按钮的文本。

这当然有效:

<button>&lsaquo;</button> 

如何将其作为变量传递?

您可以使用 {@html ...} tag

<button>{@html text}</button>

但是,如果字符串包含用户输入的值,则需要小心使用此方法。

@html 很危险,可能会导致安全漏洞。使用此解码功能。

const decode = function (html) {
    const txt = document.createElement('textarea');
    txt.innerHTML = html;
    return txt.value;
};

在渲染文本之前。 Here 是一个完整的 svelte repl 工作示例。