是什么导致错误“无法在 'TreeWalker' 上设置 'currentNode' 属性”?

ndv*_*dvo 0 typescript lit-element lit-html

在使用 Lit-Element 和 Typescript 构建 WebComponent 时,我收到了这个相当神秘的错误消息。

当我尝试将checked属性设置为元素时发生错误。

罪魁祸首是类似的东西:

    return html`<input type="radio" ${(testValue==0 ? "checked":"")}>`
Run Code Online (Sandbox Code Playgroud)

错误信息是

TypeError: Failed to set the 'currentNode' property on 'TreeWalker': The provided value is not of type 'Node'.
Run Code Online (Sandbox Code Playgroud)

ndv*_*dvo 5

经过一番研究,我终于明白了这个问题。我想我可以将模板字符串基本上视为一种模板语言。不要将其视为命令(“打印此,打印彼”),而应将其视为由函数解释的描述。当然,您需要了解解释器功能。

就我而言,我试图有条件地呈现一个属性。也就是说,如果满足条件,则打印此内容,如果不满足,则不打印任何内容。

文档对这个问题非常清楚:https : //lit-html.polymer-project.org/guide/writing-templates#bind-to-attributes

在我的情况下的解决方案是:

    return html`<input type="radio" ?checked=${(testValue==0)}>`
Run Code Online (Sandbox Code Playgroud)