vue使用WangEditor富文本编辑器(批量上传图片到服务器)

最近要写一个官网,但是这个官网要写成活的可配置的项目,于是要先写一后台管理系统,用来配置官网的菜单和页面,配置菜单自然是vue+ElementUI表格表单的增删改查,但是配置页面就要有排版、样式等等,所以想到了富文本,查了一下决定使用WangEditor这个富文本编辑器,轻量级方便上手,功能齐全:

官网:http://www.wangEditor.com
文档:http://www.kancloud.cn/wangfupeng/wangeditor3/332599
源码:http://github.com/wangfupeng1988/wangEditor

首先下载:

npm install wangeditor --save

然后在src文件夹下新建组件文件:
在这里插入图片描述
然后在父组件中引入使用:
在这里插入图片描述
接收字段:
在这里插入图片描述
使用:

<editor-bar v-model="detail1" :isClear="isClear" @change="changeEditor1"></editor-bar>

changeEditor1事件:

changeEditor1 (res) {
  console.log(res)        // 这个res就是实时输入的富文本的内容,打印出来是实时输入的字符串html
},

这个editoritem.vue文件内容如下:

<template lang="html">
  <div class="editor">
    <div ref="toolbar" class="toolbar">
    </div>
    <div ref="editor" class="text">
    </div>
    <!-- <el-button type="primary" @click="getHtml">点我获取</el-button> -->
  </div>
</template>

<script>
  import axios from 'axios'
  import E from 'wangeditor'
  export default {
    name: 'editoritem',
    data() {
      return {
        // uploadPath,
        editor: null,
        info_: null
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    props: {
      value: {
        type: String,
        default: ''
      },
      isClear: {
        type: Boolean,
        default: false
      }
    },
    watch: {
      isClear(val) {
        // 触发清除文本域内容
        if (val) {
          this.editor.txt.clear()
          this.info_ = null
        }
      },
      value: function(value) {
        if (value !== this.editor.txt.html()) {
          this.editor.txt.html(this.value)
        }
      }
      //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
    },
    mounted() {
      this.seteditor()
      this.editor.txt.html(this.value)
    },
    methods: {
      getHtml () {
          console.log(this.editor.txt.html())
      },
      seteditor() {
        // http://192.168.2.125:8080/admin/storage/create
        this.editor = new E(this.$refs.toolbar, this.$refs.editor)
        this.editor.customConfig.uploadImgShowBase64 = false // base 64 存储图片,如果这个参数设置为true的话,就不用配置服务器端上传地址
        this.editor.customConfig.showLinkImg = false  //   禁止上传网络图片
        this.editor.customConfig.uploadImgServer = '/api/file/uploadFiles'// 这是服务器端上传图片的接口路径
        this.editor.customConfig.uploadImgHeaders = {'Content-Type': 'multipart/form-data' }// 自定义 header  上传文件格式为file文件,用form-data格式
        this.editor.customConfig.uploadFileName = 'file' // 后端接受上传文件的参数名
        this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
        this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上传 6张图片
        this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间

        // 配置菜单
        this.editor.customConfig.menus = [
          'head', // 标题
          'bold', // 粗体
          'fontSize', // 字号
          'fontName', // 字体
          'italic', // 斜体
          'underline', // 下划线
          'strikeThrough', // 删除线
          'foreColor', // 文字颜色
          'backColor', // 背景颜色
          'link', // 插入链接
          'list', // 列表
          'justify', // 对齐方式
          'quote', // 引用
          'emoticon', // 表情
          'image', // 插入图片
          'table', // 表格
          'video', // 插入视频
          'code', // 插入代码
          'undo', // 撤销
          'redo', // 重复
          'fullscreen' // 全屏
        ]

        this.editor.customConfig.uploadImgHooks = {
          fail: (xhr, editor, result) => {
            // 插入图片失败回调
             console.log(xhr, editor,result)
          },
          success: (xhr, editor, result) => {
            // 图片上传成功回调
            console.log(xhr, editor)
          },
          timeout: (xhr, editor) => {
            // 网络超时的回调
            console.log(xhr, editor)
          },
          error: (xhr, editor) => {
            console.log(xhr, editor)
            // 图片上传错误的回调
          }
        }
        // =======================================重点如下:
        // this.editor.customConfig.uploadImgShowBase64 = false 如果这个参数设置true,也就不用下面这些了,
        this.editor.customConfig.customUploadImg = (files, insert) => {
            var formData = new FormData();
            for(var i = 0;i < files.length;i ++) {
                formData.append("file", files[i], files[i].name);      //  多张图片文件都放进一个form-data,有些小伙伴说这里应该这样写:formData.append("file[" + i + "]", files[i], files[i].name),后端才能拿到完整的图片数组,其实不然,用这个formData.getAll("file")方法恰好拿到数组,或者也可以forEach获取,有很多种办法。
            }
            axios.post('/api/file/uploadFiles', formData, {     // 上传图片接口
                  headers: {
                    'Content-Type': 'multipart/form-data',
                  },
            }).then(da => {
                  console.log(da)
                  if(da.data.code == 1000){
                        for(var j=0;j<da.data.data.length;j++){
                            insert("http://192.168.0.110:8080" + da.data.data[j].imgUrl);    //  前面这个ip地址是服务器ip,后面的imgUrl是后台返回的图片地址,拼接上就是图片路径
                        }
                  }else{
                        alert(da.data.data.msg);
                        return;
                  }
            })
        };
        // =========================================
        this.editor.customConfig.onchange = (html) => {
          this.info_ = html // 绑定当前逐渐地值
          this.$emit('change', this.info_) // 将内容同步到父组件中
        }
        // 创建富文本编辑器
        this.editor.create()
      }
    }
  }
</script>

<style lang="css">
  .editor {
    width: 100%;
    margin: 0 auto;
    position: relative;
    z-index: 0;
  }
  .toolbar {
    border: 1px solid rgb(148, 147, 147);
  }
  .text {
    border: 1px solid rgb(122, 122, 122);
    min-height: 500px;
  }
</style>

到这里就完了,创建成功,这里的难点就是上传图片的路径配置,摸索了一阵,如果你上传的图片是base64格式,也就是this.editor.customConfig.uploadImgShowBase64 = true,就没这么多麻烦,去掉配置上传路径这些代码,就ok了,但是这个是有弊端的,比如你的图片很多,很大,那你想想,这代码多长,所以还是用file文件格式比较靠谱一些。

官网github上有vue的demo,直接下载下来就可以用,多看看文档。


http://www.niftyadmin.cn/n/1845226.html

相关文章

uni-app图片列表预览大图

首先确定预览是新写一个页面还是搞一个弹窗&#xff0c;随你&#xff0c;我是新开一个页面的&#xff0c;然后在点击图片准备预览的时候&#xff0c;需要的参数有&#xff1a; 1&#xff0c;当前点击图片的下标&#xff0c; 2&#xff0c;当前点击图片的列表数组 点击的时候将这…

uni-app购物车-全选、反选、逐个选中

这里要实现的需求是&#xff1a; 1&#xff0c;点击全选&#xff0c;则全选&#xff0c;再次点击&#xff0c;则取反 2&#xff0c;全选状态下&#xff0c;取消某一个商品&#xff0c;则全选状态消失 3&#xff0c;有些选中&#xff0c;有些没选的状态下&#xff0c;勾选上所有…

uni-app购物车-价格、数量计算

1&#xff0c;购物车所有已勾选商品的总价、总数计算 2&#xff0c;单个商品的总价、总数计算 // carArr是购物车商品列表 computed: {// 已勾选所有商品的总价和总数计算&#xff1a;// 总价totalPrice () {let totalP 0this.carArr.map(item > {return item.checked? …

linux文本操作

grep -i 忽略大小写 -v 反向查找 -n 输出的同时打印行号 -w 强制 PATTERN 仅完全匹配字词 -E 扩展的正则表达式 -F 无视正则表达式字符awk # 1、awk可以有三个代码块&#xff0c;BEGIN块&#xff08;仅最开始运行一次&#xff09;&#xff0c;普通块&#xff08;每行运行一次…

uni.request的封装async/await

我看有一部分人用flyio这个库封装的&#xff0c;但是我看了官方是支持有限&#xff1a; 后面关于它的封装我会再写的&#xff0c;这篇封装就用原生的uni.request封装&#xff0c;只不过原生的没有请求拦截器和响应拦截器&#xff0c;只能自己判断各种状态&#xff1a; 首先&…

微信公众号验签过程详解--前端和后端的操作

项目是vue写的&#xff0c;首先下载引入依赖&#xff1a; npm install weixin-jsapi在要用的页面引入&#xff1a; import wx from weixin-jsapi关于很多人纠结的jssdk还是jsapi看看这篇博客 1&#xff0c; 这里前端要做的就是&#xff1a;请求后台给的验签接口&#xff0c…

ios验签报错,微信公众号ios和android验签的区别

首先看看验签代码&#xff0c;首先说明&#xff0c;这是vue项目用的是history模式&#xff1a; ios ios验签的时候&#xff0c;只认入口页的地址url&#xff0c;包括参数&#xff08;比如&#xff1a;http://1454657xn5.imwork.net/?tokendsj54958490nfdgnjg&#xff09;原…

解决后加载页面赋值不渲染问题

1.问题原因&#xff1a;当前页面刷新&#xff0c;会关闭主页面&#xff0c;之后当前页面跳转到主页面并且赋值给主页面进行渲染&#xff0c;因为跳转过程中&#xff0c;赋值与页面加载同时进行&#xff0c;页面回流和重绘较多&#xff0c;可能造成资源阻塞&#xff0c;没有及时…