最近项目使用到富文本,在一番谈论下决定用wangeditor富文本
- 使用 npm 安装
npm i wangeditor --save
- 使用 cdn 安装
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
接着在需要的项目组件中引入(我们将在mounted钩子写入)
<div id="editor">
wangEditor富文本编辑器
</div>
mounted() {
const editor = new wangEditor('#editor')
//获取富文本内容
editor.config.onchange = (html) => {
this.editorContent = html
}
editor.create()
}
- 此处有个坑 v3版本是通过editor.customConfig获取的,本项目采用v4版本,所以是通过editor.config来获取富文本内容,在百度上很多帖子则是v3版本,这点需要避雷,看清楚自己项目是安装v3版本还是v4版本