vue中使用wangeditor配置上传图片时this指向问题

<template>
  <div class="app-container">
    <div style="border: 1px solid #ccc;">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :default-config="toolbarConfig"
        :mode="mode"
      />
      <Editor
        v-model="html"
        style="height: 500px; overflow-y: hidden;"
        :default-config="editorConfig"
        :mode="mode"
        @onCreated="onCreated"
      />
    </div>
    <el-button type="primary" style="margin-top:3%;" @click="saveChange">保存修改</el-button>
  </div>
</template>

<script>
import axios from 'axios'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default {
  name: 'AboutDetail',
  components: { Editor, Toolbar },
  inject: ['reload'],
  data() {
    return {
      insertImg: [],
      lastImg: [],
      tableData: [],
      id: 0, // 内容的id
      base_url: process.env.VUE_APP_BASE_API,
      Authorization: 'Bearer ' + this.getCookieValue(document.cookie, 'jWTToken'),

      editor: null,
      html: '<p>hello</p>',
      toolbarConfig: { },
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            server: process.env.VUE_APP_BASE_API + 'Management/EditorUploadImg',
            fieldName: 'file',
            maxNumberOfFiles: 1,
            headers: {
              Authorization: 'Bearer ' + this.getCookieValue(document.cookie, 'jWTToken')
            },
            // 自定义插入图片
            customInsert: (res, insertFn) => {
              if (res.code === 200) {
                insertFn(this.base_url + res.url)
                this.insertImg.push(res.url)
                console.log(this.insertImg)
              } else {
                alert(res.msg)
              }
            }
          }
        }
      },
      mode: 'default' // or 'simple'
    }
  },

  created() {
    // 请求list
    axios.get(process.env.VUE_APP_BASE_API + 'Management/GetAboutDetail', {
      headers: {
        Authorization: this.Authorization // 替换为你的token
      }
    }).then(response => {
      if (response.data !== null) {
        this.id = response.data[0].id
        this.html = response.data[0].content
      }
      // console.log(response.data)
    })
  },
  // 页面销毁时要销毁编辑器
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时,及时销毁编辑器
  },
  mounted() {
  },
  methods: {
    getCookieValue(cookies, name) {
      const cookieArr = cookies.split(';')
      for (let i = 0; i < cookieArr.length; i++) {
        const cookiePair = cookieArr[i].split('=')
        const cookieKey = cookiePair[0].trim()
        const cookieVal = cookiePair[1].trim()

        if (cookieKey === name) {
          return cookieVal
        }
      }

      return null
    },
    // 创建编辑器
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
    },
    // 上传图片
    upLoadImg(file) {
      axios.post(this.base_url + 'Management/EditorUploadImg', { 'file': file }, {
        headers: {
          Authorization: this.Authorization // 替换为你的token
        }
      }).then(response => {
        if (response.code === 200) {
          this.insertImg.push(response.url)
          // 插入图片
          // 将图片url插入到编辑器中
          this.$refs.editor.txt.insertImage(response.url)
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    // 保存修改
    saveChange() {
      // 获取所有的图片
      var all = this.editor.getElemsByType('image')
      // 获取原来插入的图片
      var insert = this.insertImg
      console.log('insert----' + insert)
      console.log('all----' + all)
    }
  }
}

</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

我需要把上传成功后的图片url 保存到insertImg中,以便于最后在保存时比对前后图片的变化,并将删除的图片从硬盘中删除掉,免得占用资源。

在data下的this指向如果直接使用就会undfined,所有要使用箭头函数来确保this的正确指向

代码如下:

 // 自定义插入图片
            customInsert: (res, insertFn) => {
              if (res.code === 200) {
                insertFn(this.base_url + res.url)
                this.insertImg.push(res.url)
                console.log(this.insertImg)
              } else {
                alert(res.msg)
              }
            }

这样就确保了this的正确指向。

如果你也遇到同样的问题,该方法对你有用记得点赞哦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值