vue引入wangEditor富文本编辑器

1.官网api

https://www.wangeditor.com/doc/

2.vue引入

npm i wangeditor --save
创建wang-editor.vue组件

<template>
  <div>
    <div ref="toolbar" class="toolbar"></div>
    <p> ---------------</p>
    <div ref="editor" class="text"></div>
  </div>
</template>

<script>
  import E from 'wangeditor'

  export default {
    name: 'wangEditor',
    data() {
      return {
        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为编辑框输入的内容即父组件的v-model的值
      value: function(value) {
        if (value !== this.editor.txt.html()) {
          this.editor.txt.html(this.value)
        }
      }
    },
    mounted() {
      this.initEditor()
      this.editor.txt.html(this.value)
    },
    // 销毁编辑器
    beforeDestroy() {
      this.editor.destroy()
      this.editor = null
    },
    methods: {

      initEditor() {
        this.editor = new E(this.$refs.toolbar, this.$refs.editor)
        this.editor.config.uploadImgShowBase64 = false // 是否设置base64存储图片
        this.editor.config.uploadImgServer = '/api/upload/uploadImg'// 配置服务器端地址
        this.editor.config.uploadImgHeaders = { }// 自定义 header
        this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
       // this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
        this.editor.config.uploadImgMaxLength = 3 // 限制一次最多上传 3 张图片
       // this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
        this.editor.config.showLinkImg = false //隐藏插入网络图片的功能
        // 设置编辑区域高度为 500px
        this.editor.config.height = 500

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

        this.editor.config.uploadImgHooks = {
          // 上传图片之前
          before: function(xhr) {
            console.log(xhr)
            // 上传之前的一些判断操作,可阻止图片上传
           /* return {
              prevent: true,
              msg: '需要提示给用户的错误信息'
            }*/
          },
          // 图片上传并返回了结果,图片插入已成功
          success: function(xhr) {
            console.log('success', xhr)
          },
          // 图片上传并返回了结果,但图片插入时出错了
          fail: function(xhr, editor, resData) {
            console.log('fail', resData)
          },
          // 上传图片出错,一般为 http 请求的错误
          error: function(xhr, editor, resData) {
            console.log('error', xhr, resData)
          },
          // 上传图片超时
          timeout: function(xhr) {
            console.log('timeout')
          },
          // 图片上传并返回了结果,想要自己把图片插入到编辑器中
          // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
          customInsert: function(insertImgFn, result) {
            // result 即服务端返回的接口
            console.log('customInsert', result)
            //insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
            //我的后端接口返回的格式为{code:0,data:'url地址'}
            insertImgFn(result.data)
          }
        }
        this.editor.config.onchange = (html) => {
          this.info = html
          this.$emit('change', this.info) // 将内容同步到父组件中
        }
        // 创建富文本编辑器
        this.editor.create()
      }
    }
  }
</script>

<style lang="less">
  .toolbar {
    border: 1px solid #ccc;
  }
  .text {
    border: 1px solid #ccc;
    height: 400px;
  }
</style>

3.父组件调用

<wangeditor v-model="details" :isClear="isClear" @change="changeEditor"></wangeditor>

 import wangeditor from '@/components/wang-editor'
 export default {
    name: "p-editor",
    data() {
      return {
        details: '', // 双向同步的变量
        isClear: false,
        id:'1'
      };
    },
    components: {
      wangeditor
    },
    mounted(){
      this.getInfoById(this.id)
    },
    methods:{
      //根据id查询数据
      getInfoById(id){
        axios.post('/api/getById', {id:id}).then(res => {
         //回显赋值给富文本编辑器
          this.details = res.data.content
        })
      },
      //保存
      save(item){
        let content = this.details
        axios.post('/api/save', {id:this.id,content:content}).then(res => {
          if(res.code === 0){
            this.$message.success('保存成功');
            this.getInfoById(id)
          }
        })
      },
     
		 //子组件编辑器的值改变同步赋值
      changeEditor(val) {
       // console.log(val)
        this.details = val
      },

    }

  };
    
 

后端接口代码及使用vue-quill编辑器链接如下 :
https://blog.csdn.net/ziyu_nuannuan/article/details/121675984

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wangEditor是一款基于JavaScript和CSS开发的Web富文本编辑器,它具有轻量级、简洁、易用的特点。相比于普通的文本编辑器,富文本编辑器可以输入超越文本的数据内容,包括上传图片、输入表情、字体大小字号调整、颜色设置、对齐方式等功能操作。[1] 在使用wangEditor富文本编辑器时,首先需要在代码中引入相关的文件。可以在editor.vue文件中添加以下代码: ```html <template> <div> <div ref="editor" style="text-align:left"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'MyEditor', data() { return { editorContent: '', editor: null } }, props: { value: { type: String, required: true } }, model: { prop: 'value' }, methods: { getContent: function () { alert(this.editorContent) }, _initEditor(that) { var editor = new E(this.$refs.editor) editor.config.zIndex = 100 editor.create() that.editor = editor } }, mounted() { this._initEditor(this) setTimeout(() => { this.editor.txt.html(this.value) }, 1000) } } </script> <style scoped> </style> ``` 然后,可以在需要使用富文本编辑器的地方注册组件,并创建编辑器。可以使用以下代码: ```html <!-- 首先给出一个div,并设id属性 --> <div id="div1"></div> <script type="text/javascript"> var E = window.wangEditor var editor = new E('#div1') // 创建编辑器 editor.create() </script> ``` 以上是使用wangEditor富文本编辑器的基本介绍和使用方法。[1][2][3]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值