vue结合el-upload与el-input实现自制简易富文本编辑器,支持上传图片,需要有个接口把本地图片转成在线链接

vue页面点击修改页面循环渲染元素中的数据的值不生效,我的建议是双管齐下

 gotoEdit(i) {
      this.$set(this.reportList[i], "editStatus", true);
      this.$forceUpdate();
    },

vue结合el-upload与el-input实现自制简易富文本编辑器,支持上传图片,需要有个接口把本地图片转成在线链接

展示 

编辑

  <template v-if="pageStatus !== 'see'">
                  <span
                    v-if="item.editStatus"
                    @click="
                      (item.editStatus = false),
                        setDetailOrAdd('dontCloseWindow')
                    "
                    title="确定"
                    class="buttonStyle over"
                  ></span
                  ><span
                    v-else
                    @click="gotoEdit(i)"
                    title="编辑"
                    class="buttonStyle edit"
                  ></span> </template
                >
              </div>
              <el-input
                class="main-part"
                v-if="item.editStatus"
                v-model="item.propertyValue"
                placeholder="请输入内容"
                style="width: 100%;height: 300px;"
                type="textarea"
                show-word-limit
              ></el-input>
              <div v-else class="main-part" v-html="item.propertyValue"></div>
              <!-- <div
              v-if="item.editStatus"

              class="main-part"
              :contenteditable="pageStatus !== 'see' ? true : false"
              v-html="item.propertyValue"
            ></div> -->

              <div v-if="item.editStatus" class="add-button">
                <el-upload
                  class="upload"
                  action="/cbrc/file/upload"
                  :on-success="
                    (response, file, fileList) =>
                      upsuccessImg(response, file, fileList, i)
                  "
                  :headers="header"
                  :show-file-list="false"
                  :on-remove="deletefileImg"
                  :before-upload="handleChangeImg"
                  :file-list="imgList"
                  ref="uploadImg"
                >
                  <el-button type="primary" class="blue"
                    >添加图片<i class="el-icon-upload el-icon--right"></i
                  ></el-button>
                </el-upload>
              </div>
 gotoEdit(i) {
      this.$set(this.reportList[i], "editStatus", true);
      this.$forceUpdate();
    },
    upsuccessImg(response, file, fileList, index) {
      if (response.code == 200) {
        this.attachmentLists = fileList;
        if (this.attachmentLists && this.attachmentLists[0].response) {
          this.attachmentLists[0].response.data.forEach(item => {
            this.imgList.push({
              name: item.attachName,
              url: item.attachPath
            });
            // 上传成功就拼接成富文本
            const setOver =
              this.reportList[index].propertyValue +
              ('<br /><img src="/cbrc' + item.attachPath + '" ><br />');
            this.$set(this.reportList[index], "propertyValue", setOver);

          });
        }
 this.imgList=[];
      } else {
        this.$message.error("图片添加失败");
      }
    },
    //附件删除
    deletefileImg(file, fileList) {
      var url = "";
      if (file.url) {
        url = file.url;
      } else {
        url = file.response.data[0].attachPath;
      }
      this.$http({
        method: "GET",
        url: "/cbrc/file/del?filePath=" + url
      }).then(res => {
        if (res) {
          this.fileList = [];
          this.fileName = "";
          this.attachmentLists = fileList;
          this.$message.success("删除成功");
        }
      });
    },
    handleChangeImg(file, fileList) {
      var size = file.size / 1024 / 1024;
      if (size > 10) {
        this.$message.warning("上传文件大小不能大于10M");
        return false;
      }
    },

Element UI的vue-quill-editor富文本编辑器支持插入图片,但是默认的图片上传功能可能不能满足所有需求,需要进行自定义。 首先,在vue-quill-editor的配置中添加`imageHandler`方法,用于处理图片上传: ```javascript <template> <quill-editor v-model="content" :options="editorOption"></quill-editor> </template> <script> import { quillEditor } from &#39;vue-quill-editor&#39; export default { components: { quillEditor }, data () { return { content: &#39;&#39;, editorOption: { imageHandler: this.imageHandler // 添加imageHandler方法 } } }, methods: { imageHandler () { // 处理图片上传 } } } </script> ``` 然后,可以使用第三方上传组件(如`el-upload`)进行图片上传上传完成后将图片地址返回给`quill-editor`。可以在`imageHandler`方法中实现该逻辑: ```javascript <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleSuccess" :show-file-list="false" :headers="headers" ref="upload" > <el-button size="small" type="primary">上传图片</el-button> </el-upload> </div> </template> <script> import { quillEditor } from &#39;vue-quill-editor&#39; import { mapGetters } from &#39;vuex&#39; export default { components: { quillEditor }, data () { return { content: &#39;&#39;, editorOption: { imageHandler: this.imageHandler }, uploadUrl: &#39;https://www.example.com/upload&#39; // 图片上传地址 } }, computed: { ...mapGetters([&#39;getToken&#39;]) // 获取token }, methods: { imageHandler () { const self = this const uploadImg = this.$refs.upload uploadImg.click() uploadImg.$refs.input.onchange = function () { const file = uploadImg.$refs.input.files[0] const formData = new FormData() formData.append(&#39;file&#39;, file) self.$axios.post(self.uploadUrl, formData, { headers: { &#39;Authorization&#39;: self.getToken // 设置token } }).then(res => { const url = res.data.url // 获取图片地址 const editor = self.$refs.editor.quill // 获取quill对象 const index = (editor.getSelection() || {}).index || editor.getLength() editor.insertEmbed(index, &#39;image&#39;, url) // 插入图片 }).catch(err => { console.log(err) }) } } } } </script> ``` 在这个例子中,使用了`el-upload`组件进行图片上传上传完成后将图片地址返回给`quill-editor`。在`imageHandler`方法中,通过`this.$refs.editor.quill`获取到了`quill`对象,然后调用`insertEmbed`方法插入图片。 需要注意的是,由于`quill`对象是异步创建的,所以需要在`mounted`生命周期中获取到`quill`对象才能进行图片插入。可以使用`$nextTick`方法来确保获取到了`quill`对象: ```javascript <template> <quill-editor v-model="content" :options="editorOption" ref="editor"></quill-editor> </template> <script> import { quillEditor } from &#39;vue-quill-editor&#39; export default { components: { quillEditor }, data () { return { content: &#39;&#39;, editorOption: { imageHandler: this.imageHandler } } }, mounted () { this.$nextTick(() => { // 获取quill对象 const editor = this.$refs.editor.quill // 在quill对象中添加图片上传功能 editor.getModule(&#39;toolbar&#39;).addHandler(&#39;image&#39;, () => { this.$refs.upload.click() }) }) }, methods: { imageHandler () { // 处理图片上传 } } } </script> ``` 在这个例子中,通过`editor.getModule(&#39;toolbar&#39;).addHandler`方法,在`quill`对象中添加了一个`image`按钮,点击该按钮时触发了上传图片的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JianZhen✓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值