javascript - 将 unsafeHTML 与 lit-element 一起使用时出错
问题描述
这是我的代码:
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 }); }
我的代码很简单,但我仍然遇到错误,所以任何人都可以向我建议如何使其工作。
解决方案
此类错误通常是由同一项目中多个版本的 lit-html 交互引起的。一个常见的场景是当您开始使用LitElement
(内部使用 的版本lit-html
)然后lit-html
单独安装以便能够使用内置指令时。这有时会导致重复,可以通过运行轻松修复
$ npm dedupe
此外,对于纱线用户:
$ yarn install --flat
在这种情况下可以提供帮助。
推荐阅读
- javascript - 为用户提供一个 excel 文件,以便他们可以在 react js 中将其下载到他们的系统中
- java - Java IBM Lotus 连接:NotesFactory 创建会话失败并出现“UnsatisfiedLinkError”
- ios - 如何在 Swift 的函数中将表达式作为参数传递?
- java - Eureka Server 按配置文件的多个实例
- html - shell 脚本:将硬编码的超链接转换为通用链接
- android - Android 9 (Pie) 中的 java.lang.NoClassDefFoundError
- java - 跨存储库的 Bitbucket 管道缓存依赖项
- java - 在 ADF 应用程序中找不到 JBO-29000 InvocationTarget, msg=null 的位置
- python - 如何创建一个数据框聚合(分组?)一个只包含字符串的数据框
- php - 在真实主机上使用 UTF-8 将表数据导出到 CSV 文件