【发布时间】:2020-07-23 10:39:21
【问题描述】:
这是我的代码:
import {customElement, LitElement, html, property, css} from 'lit-element';
import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
@customElement('my-component')
export class myComponent extends LitElement {
render() {
const markup = '<div>Some HTML to render.</div>';
return html`
${unsafeHTML(markup)}
`;
}
}
但是当我在浏览器上运行时,我收到如下错误:
part => { if (!(part instanceof NodePart)) { throw new Error('unsafeHTML can only be used in text bindings'); } const previousValue = previousValues.get(part); if (previousValue !== undefined && isPrimitive(value) && value === previousValue.value && part.value === previousValue.fragment) { return; } const template = document.createElement('template'); template.innerHTML = value; // innerHTML casts to string internally const fragment = document.importNode(template.content, true); part.setValue(fragment); previousValues.set(part, { value, fragment }); }
我的代码很简单,但我仍然遇到错误,所以任何人都可以向我建议如何使它工作。
【问题讨论】:
-
你提到的错误显示在指令的 in place 中,而不是在控制台中,不是吗?
-
是的,它显示在渲染它的 DOM 中。
标签: javascript web-component lit-element lit-html