vue 使用 wangeditor 富文本编辑器

news/2024/10/23 9:26:10/

wangeditor 是一个轻量级 web 富文本编辑器,配置方便,使用简单。

1)安装 wangeditor

终端安装 wangeditor 库:

yarn add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save

2)页面绑定

创建一个 xxx.vue 页面,在 div 上设置 id

<template>  <div><div id="editor" style="background: #ffffff;"></div></div>
</template>  

3)页面引入

xxx.vue 页面 引入wangeditor

import E from 'wangeditor'

4)配置wangeditor的配置项

在生命周期 mounted 里配置wangeditor 的配置项:

  mounted () {this.initialEditor()},methods: {initialEditor () {// 绑定div上的 editorthis.editor = new E('#editor')// 获取输入的内容this.editor.config.onchange = html => {this.form.content = html;};// 上传图片到服务器this.editor.config.customUploadImg = (files, insertImgFn) => {// files 是 input 中选中的文件列表// insertImgFn 是获取图片 url 后,插入到编辑器的方法// 上传代码返回结果之后,将图片插入到编辑器中const params = new FormData();params.append("file", files[0]);this.$http({url: this.uploadUrl,method: "post",data: params}).then(res => {if (res.url) {// 上传图片,返回结果,将图片插入到编辑器中insertImgFn(res.url);}})}// 上传图片到服务器,是否以base64形式this.editor.config.uploadImgShowBase64 = falsethis.editor.config.pasteFilterStyle = falsethis.editor.config.zIndex = 100this.editor.config.colors = ['#ffffff', '#000000', '#eeece1', '#1c497d', '#4d80bf', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', "#c00000", '#ff0000', '#ffc000', '#ffff00', '#92d050', '#00b050', '#00b0f0', '#0070c0', '#002060', '#7030a0']// 创建一个富文本编辑器this.editor.create();}
}

5)完整代码

xxx.vue 完整代码:

<template>  <div><div id="editor" style="background: #ffffff;"></div></div>
</template>  
<script>
import E from 'wangeditor'
export default {name: 'test',data () {return {form: { content: '' },baseURI: 'xxx',uploadUrl: `${baseURI}/upload?token=xxx`,}},mounted () {this.initialEditor()},methods: {initialEditor () {// 绑定div上的 editorthis.editor = new E('#editor')// 获取输入的内容this.editor.config.onchange = html => {this.form.content = html;};// 上传图片到服务器this.editor.config.customUploadImg = (files, insertImgFn) => {// files 是 input 中选中的文件列表// insertImgFn 是获取图片 url 后,插入到编辑器的方法// 上传代码返回结果之后,将图片插入到编辑器中const params = new FormData();params.append("file", files[0]);this.$http({url: this.uploadUrl,method: "post",data: params}).then(res => {if (res.url) {// 上传图片,返回结果,将图片插入到编辑器中insertImgFn(res.url);}})}// 上传图片到服务器,是否以base64形式this.editor.config.uploadImgShowBase64 = falsethis.editor.config.pasteFilterStyle = falsethis.editor.config.zIndex = 100this.editor.config.colors = ['#ffffff', '#000000', '#eeece1', '#1c497d', '#4d80bf', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', "#c00000", '#ff0000', '#ffc000', '#ffff00', '#92d050', '#00b050', '#00b0f0', '#0070c0', '#002060', '#7030a0'],// 配置菜单this.editor.config.menus = ["head", // 标题"bold", // 粗体"fontSize", // 字号"fontName", // 字体"lineHeight", // 行高"italic", // 斜体"underline", // 下划线"strikeThrough", // 删除线"foreColor", // 文字颜色"backColor", // 背景颜色"link", // 插入链接"list", // 列表"justify", // 对齐方式"quote", // 引用"emoticon", // 表情"image", // 插入图片"table", // 表格"video", // 插入视频"code", // 插入代码"undo", // 撤销"redo", // 重复"fullscreen" // 全屏]// 富文本内容this.editor.txt.html()// 创建一个富文本编辑器this.editor.create();}},beforeDestroy() {let editor = this.editorif (editor == null) return// 调用销毁 API,组件销毁时,及时销毁 editor ,重要!!!editor.destroy()editor = null}
}
</script>

页面效果:
在这里插入图片描述


http://www.ppmy.cn/news/651675.html

相关文章

【wangEditor 5】在线解析本地word,将内容展现在wangEditor中

最近有个问题&#xff0c;在word中复制的多张图片&#xff0c;或者同时复制了图片和文字&#xff0c;图片粘贴到编辑器中不显示。 原因很简单&#xff1a;浏览器无法直接访问本地资源下的文件&#xff0c;切记这一点&#xff0c;不要试图用浏览器直接访问你本地文件。因为从技…

wangEditor富文本编辑器获取html内容

该示例为官方示例&#xff0c;代码仅供参考。 功能&#xff1a;获取html内容&#xff0c;目录结构如下&#xff1a; get-html.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA…

vue 使用wangEditor

1、npm install wangeditor/core wangeditor/editor wangeditor/editor-for-vue 2、封装组件 MyEditor.vue, &#xff08;这里是通过props content 将展示内容传入&#xff09; <template><div><div style"border: 1px solid #ccc; margin-top: 10px&quo…

vue2使用wangEditor

vue2使用wangEditor 前几天一个月薪35k的兄弟&#xff0c;给我推了一个人工智能学习网站&#xff0c;看了一段时间挺有意思的。包括语音识别、机器翻译等从基础到实战都有&#xff0c;很详细&#xff0c;分享给大家。大家及时保存&#xff0c;说不定啥时候就没了。 效果 wang…

wangEditor的一些坑记录

1、在vue中使用wangEditor 网上的资料一堆&#xff0c;但基本都是wangEditor3的运用&#xff0c;wangEditor4只需要把customConfig 改成config即可。 例如&#xff1a;关于在vue项目中使用wangEditor 2、标题样式、斜体样式不生效 感谢文章&#xff1a;https://blog.csdn.ne…

vue2 cli使用wangEditor ( wangEditor安装依赖, wangEditor.vue文件实例, wangEditor工具栏配置的key列表 )

wangEditor官网地址https://www.wangeditor.com/ 安装依赖 yarn add wangeditor/editor # 或者 npm install wangeditor/editor --saveyarn add wangeditor/editor-for-vue # 或者 npm install wangeditor/editor-for-vue --save xxx.vue文件示例 <template><div s…

富文本编辑器wangEditor回显问题

回显时渲染dom报错问题&#xff1a; 富文本编辑器在回显html时偶尔会报以下错误&#xff1a; Error in v-on handler: "Error: Cannot find a descendant at path [1,1,0] in node: {“children”:[],“operations”:[{“type”:“remove_node”,“path”:[0],“node”:…

wangeditor安装

在没有搭建Vue-CLI的情况下安装wangeditor&#xff0c;官网原代码输入命令行后报错。 解决&#xff1a;把去掉&#xff0c;即可安装成功。 即输入npm install wangeditor --save