【发布时间】:2021-11-03 10:23:09
【问题描述】:
我正在尝试在我的 NuxtJS 应用程序中使用 DOMPurify 包将 HTML 解析为干净且安全的字符串,以便在 UI 中呈现。在渲染使用包的页面时,出现以下错误:
dompurify__WEBPACK_IMPORTED_MODULE_1___default.a.sanitize is not a function
关于如何解决此问题的任何建议?我在此处的代码框中有此代码: https://codesandbox.io/s/reverent-bardeen-kh4wg?file=/pages/index.vue:0-2868
我已将包导入到我的单个文件组件中,如下所示:
<template>
......cut unnecessary code for this example...
<textarea
id="title_input"
v-model.trim="formValues.title"
class="form-control form-control-lg border-0 fw-bold lh-1 fs-1 mb-3"
placeholder="New post title here..."
maxlength="80"
rows="2"
minlength="6"
autocomplete="off"
required
aria-describedby="titleHelpBlock"
></textarea>
<textarea
id="content_input"
v-model.trim="formValues.content"
class="form-control border-0 h-100"
rows="3"
minlength="30"
maxlength="1000"
autocomplete="off"
placeholder="Write your post here..."
required
aria-describedby="contentHelpBlock"
></textarea>
.....
</template>
<script>
import { debounce } from "lodash";
import DOMPurify from "dompurify";
import marked from "marked";
export default {
name: "UploadForm",
data() {
return {
formValues: {
title: "New post title here...",
content: "Write your post here...",
},
};
},
computed: {
compiledMarkdown() {
// only need the HTML profile, not SVG andMathML stuff
const clean = DOMPurify.sanitize(this.formValues.title, {
USE_PROFILES: { html: true },
});
return marked(clean);
},
},
methods: {
update: debounce(function (e) {
this.input = e.target.value;
}, 300),
updateTitle: debounce(function (e) {
this.formValues.title = e.target.value;
}, 300),
updateContent: debounce(function (e) {
this.formValues.content = e.target.value;
}, 300),
},
};
</script>
【问题讨论】:
-
我从未直接使用过
dompurify。同时,Vue 包装器的效果非常好:npmjs.com/package/vue-dompurify-html 试一试,让我们知道它是否更好。 -
好的!我会回复你的
-
同样的问题!
VueDOMPurifyHTML is not defined。更新了我的代码框。我没有正确访问插件吗? -
现在好像没事了。我以为我需要将
VueDOMPurifyHTML添加到计算中,但删除它似乎可以解决它。我只需要在v-dompurify-html指令中使用它。 -
是的,import + 指令足以让它工作。