【Vue】集成富文本编辑器

这文章使用的是wangeditor插件,官网地址:wangEditor,这个比较简单

安装

复制代码
npm i wangeditor --save

使用

复制代码
<div id="editor"></div>

import E from "wangeditor"

const editor = new E("#editor")
editor.create()

//或者
export default {
data() {
   return {
     editor: null
   }
}
this.editor = new E(`#editor`)//设置参数
this.editor.create() //创建

报错:

原因是在打开表单的时候元素还没创建好,所以找不到

解决方法:

使用NextTick:在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

复制代码
this.$nextTick(() => {
        this.editor = new E(`#editor`)//设置参数
        this.editor.create() //创建
      })

销毁编辑器

关闭时销毁,不然下次打开内容还在编辑器中

复制代码
//在dialog销毁时调用
beforeDestroy() {
this.editor.destroy ()
this.editor = null
}

// 获取编辑器内容
this.editor.txt.html()

配置图片上传

配置图片上传接口

java 复制代码
 @PostMapping("/editor/upload")
    public Dict editorUpload(MultipartFile file) throws IOException {
        String originaFilename = file.getOriginalFilename();//文件原始名 a.jpg
        String mainname = FileUtil.mainName(originaFilename);//a
        String extname = FileUtil.extName(originaFilename);//jpg
        File parentFile = new File(ROOT_PATH);
        if (!FileUtil.exist(ROOT_PATH)) {
            //若当前父级目录不存在就创建一个
            FileUtil.mkdir(ROOT_PATH);
        }
        if (FileUtil.exist(ROOT_PATH + File.separator + originaFilename)) {
            //若当前上传的文件名已存在,则重命名
            originaFilename = System.currentTimeMillis() + "_" + mainname + "." + extname;//124234_a.jpg
        }
        File saveFile = new File(ROOT_PATH + File.separator + originaFilename);
        file.transferTo(saveFile);//存储到本地
        String url = "http://localhost:8080/file/download/" + originaFilename;
        Dict dict=Dict.create().set("errno",0).set("data", CollUtil.newArrayList(Dict.create().set("url",url)));
        //返回
        return dict;
    }

前端使用接口:

TypeScript 复制代码
this.editor.config.uploadImgServer = this.$baseUrl + '/file/editor/upload'
this.editor.config.uploadFileName = 'file'
this.editor.config.uploadImgHeaders = {
     token: this.user.token
}

server 接口返回格式,重要!!!

复制代码
{
"errno": 0,
"data":[
    {url:"图片地址"}
 ]
}

视频上传

TypeScript 复制代码
this.editor.config.uploadVideoServer = this.$baseUrl + '/file/editor/upload'
        this.editor.config.uploadVideoName = 'file'
        this.editor.config.uploadVideoHeaders = {
          token: this.user.token
        }

写到这里报错了,最后发现视频的格式和图片格式要求不一样,视频格式:

所以需要改一下后端接口,判断是视频还是图片,最后完整代码:

接口:

java 复制代码
@PostMapping("/editor/upload")
    public Dict editorUpload(@RequestParam MultipartFile file, @RequestParam String type) throws IOException {
        String originaFilename = file.getOriginalFilename();
        String mainname = FileUtil.mainName(originaFilename);
        String extname = FileUtil.extName(originaFilename);
        if (!FileUtil.exist(ROOT_PATH)) {
            FileUtil.mkdir(ROOT_PATH);
        }
        if (FileUtil.exist(ROOT_PATH + File.separator + originaFilename)) {
            //若当前上传的文件名已存在,则重命名
            originaFilename = System.currentTimeMillis() + "_" + mainname + "." + extname;//124234_a.jpg
        }
        File saveFile = new File(ROOT_PATH + File.separator + originaFilename);
        file.transferTo(saveFile);//存储到本地
        String url = "http://localhost:8080/file/download/" + originaFilename;
        Dict dict = Dict.create().set("errno", 0);
        if ("img".equals(type)) {
            dict = Dict.create().set("errno", 0).set("data", CollUtil.newArrayList(Dict.create().set("url", url)));
        } else if ("video".equals(type)) {
            dict = Dict.create().set("errno", 0).set("data", Dict.create().set("url", url));
        }
        //返回
        return dict;
    }

前端:

javascript 复制代码
//编辑显示
this.$nextTick(() => {
        this.editor = new E(`#editor`)//设置参数

        this.editor.config.uploadImgServer = this.$baseUrl + '/file/editor/upload'
        this.editor.config.uploadFileName = 'file'
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadImgParams = {
          type: 'img'
        }
        this.editor.config.uploadVideoServer = this.$baseUrl + '/file/editor/upload'
        this.editor.config.uploadVideoName = 'file'
        this.editor.config.uploadVideoHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadVideoParams = {
          type: 'video'
        }
        this.editor.create() //创建
        this.editor.txt.html(row.content)
      })
相关推荐
liangshanbo12153 小时前
面试题:前端工程化中 Tree Shaking的原理是什么?
前端·treeshaking
殷色玫瑰4 小时前
C++ STL:stack、queue、priority_queue 与容器适配器详解
java·开发语言·数据结构·c++·算法·visualstudio
500844 小时前
React Native for OpenHarmony 实战:三方库 react-native-url-polyfill 的鸿蒙化适配指南
javascript·react native·react.js·性能优化·electron·harmonyos
IT方大同4 小时前
java输入输出+方法
java·开发语言·python
Yyyyyy~4 小时前
【Java】String
java
补码缩写4 小时前
视频跳转触发了seeked,画面就到位了吗
前端
库拉镜像AI牛牛4 小时前
漫剧工作室量产方案:依托知漫剧 AI 短剧降本增效
大数据·服务器·前端·人工智能·语音识别
大纽耶耶4 小时前
我把 YOLO 训练做成了网页版:需求设计与技术选型
vue.js
不可能片场4 小时前
内容运营自动化 用相似度拦住撞车选题
前端·electron
静默回滚4 小时前
视频进度条跳回去,先别改事件监听
前端