javascript - 使用 vuejs 复制 innerHTML 输出值
问题描述
我目前正在学习 vue,当我对 HTML 文本进行排序时遇到了问题。这是我的组件模板,它是所见即所得的。
<template>
<div id='editor_container'>
<slot name="header" />
<div id='editor' ref='editorPost'></div>
<slot name="footer" />
</div>
</template>
从 './helpers/docFormats' 导入 { tempDOC }
我创建了简单的函数来发送数据进行测试。
templateEdit () {
const editor = this.$refs.editorPost.innerHTML = tempDOC
}
在我的 tempDOC.js 文件中,我导出字符串:
导出常量 tempDOC =Please enter your name to continue
当我将 tempDOC 中的 innerHTML 内容渲染到 $refs.editorPost(editor WYSIWYG) 中时,该值会被重复。
编辑结果:
请输入您的姓名以继续
请输入您的姓名以继续
Bellow 是检查 HTML。
<div id="editor" spellcheck="false" contenteditable="true">
<p>Please enter your name to continue</p>
Please enter your name to continue
</div>
不确定这些值是否会重复,我使用 chrome 调试应用程序,我看到这是在我的函数之后调用的。我这边没有那个代码。
this._observer = new MutationObserver(function (mutations) {
_this._handleMutations(mutations);
});
}
解决方案
为此目的使用 refs 是一种不好的做法。您应该使用Data
Vue 的功能来创建一个变量并通过 vue 更改它/达到它的值this
。
<template>
<div id='editor_container'>
<slot name="header" />
<div id='editor'>{{editorPost}}</div>
<slot name="footer" />
</div>
</template>
<script>
export default {
data() {
return {
editorPost: "this is the inner HTML",
}
},
}
</script>
现在更改editorPost
以更改innerHTML
.
推荐阅读
- profiling - 如何计算 TBB 处理指令?
- python - Keras - 将值绘制到张量板上
- python - DLL 加载失败并且导入 tensorflow CPU 版本时没有名为“_pywrap_tensorflow_internal”的模块错误无法使用“mscvp140.dll”解决
- gnuplot - 如何使用 gnuplot 创建实际的直方图
- c# - 检查卷文件系统的大小写敏感性而不写入它?
- llvm - LLVM 中的“ModulePassManagers”、“FunctionPassManager”和“BasicBlockPassManagers”有什么区别?
- python - SQLite3 如何为多对多关系创建插入语句?
- sql - 如何在 SQL 中使用 CAST 在一列/原始中显示多个“GL 编码”
- json - 我对 JSON 比较陌生,我找不到我的问题
- nginx - 带有 URL 重写和 proxy_pass 的 Nginx 转发