Vue富文本插件WangEditor使用

首先npm安装:

npm i wangeditor --save

接下来:
1、在vue项目的components文件夹中新建一个插件专门的文件夹如:wangEditorTool
2、新建一个vue文件,内容如下:

<template lang="html">
  <div class="editor">
    <div ref="toolbar" class="toolbar" />
    <div ref="editor" class="text" />
  </div>
</template>

<script>
import E from 'wangeditor'
export default {
  name: 'Editoritem',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    isClear: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // uploadPath,
      editor: null,
      info_: null
    }
  },
  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: {
    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 存储图片
      this.editor.customConfig.uploadImgServer = 'http://otp.cdinfotech.top/file/upload_images'// 配置服务器端地址
      this.editor.customConfig.uploadImgHeaders = { }// 自定义 header
      this.editor.customConfig.uploadFileName = 'file' // 后端接受上传文件的参数名
      this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
      this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上传 3 张图片
      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) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
        },
        customInsert: (insertImg, result, editor) => {
          // 图片上传成功,插入图片的回调
          // result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...]
          // console.log(result.data[0].url)
          // insertImg()为插入图片的函数
          // 循环插入图片
          // for (let i = 0; i < 1; i++) {
          // console.log(result)
          const url = 'http://otp.cdinfotech.top' + result.url
          insertImg(url)
          // }
        }
      }
      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 #ccc;
  }
  .text {
    border: 1px solid #ccc;
    min-height: 500px;
  }
</style>

最后:
在需要使用的文件中进行引用:

 <EditorTool v-model="entity.content" :is-clear="isClear" @change="changeEditor" />
 
import EditorTool from '@/components/wangEditor/wangEditor'

components: { EditorTool },

 data() {
    return {
		isClear: false,
      	detail: ''
      }
  },
changeEditor(val) {
      console.log(val)
    },
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Vue Wangeditor 是一款基于 Vue.js富文本编辑器,它可以帮助开发者快速构建出一个功能强大的富文本编辑器,支持多种格式的文本编辑,包括文字、图片、视频等。它具有易用性、可扩展性和高度定制化的特点,可以满足不同场景下的需求。同时,Vue Wangeditor 还提供了丰富的 API 和插件,方便开发者进行二次开发和定制。 ### 回答2: Vue Wangeditor 富文本编辑器是一款基于 Vue 开发的富文本编辑器组件,它采用了著名的文本富编辑器框架 Wangeditor 的核心代码,并通过 Vue 组件化的思想封装为一个易于使用和集成的 Vue 组件。它拥有良好的用户界面,支持各种基本的富文本编辑功能如字体、颜色、图片、表格、粘贴等,并提供了丰富的插件扩展接口,可用于满足不同的业务需求。下面从使用上和技术上两个方面简要介绍一下 Vue Wangeditor 富文本编辑器: 使用上,Vue Wangeditor 富文本编辑器非常易于使用。通过简单的使用导入和注册组件,就可以在 Vue 开发的项目中使用了。在页面引入组件后,只需要像使用普通的 Vue 控件一样将它放到页面中并绑定相应的数据,就可以在页面中展示可编辑的富文本内容了。而对于基本的富文本编辑功能,它提供了完整的 API 接口和事件回调,可以根据不同的业务逻辑进行扩展和优化,非常灵活方便。 技术上,Vue Wangeditor 富文本编辑器采用了 Vue 组件化开发的方式,它将富文本编辑器封装成了一个可复用的单文件组件,同时通过 Vue 的自定义事件机制和子组件通信机制来实现各种功能的交互和通信。它还通过自定义插件、自定义指令等方式提供了灵活的扩展接口,可以方便地添加各种功能扩展和优化。此外,它也使用了 Webpack 等现代前端工具来优化和打包,可以确保代码的质量和性能。总之,Vue Wangeditor 富文本编辑器是一款优秀的富文本编辑器组件,它的设计、实现和使用都非常优雅和简单,为 Vue 应用开发提供了有效的解决方案。 ### 回答3: Vue wangeditor是一款基于Vue.js开发的富文本编辑器,具有简单易用、UI美观、代码简洁等特点。该富文本编辑器支持文本、图片、视频、代码等多种格式的输入和处理,并且还具有自动保存、实时预览等实用功能。 使用Vue wangeditor富文本编辑器,可在页面中直接嵌入编辑器组件,以便用户可以直接在页面上进行富文本编辑。同时,该富文本编辑器还支持多种主题、多种语言、以及自定义扩展等功能,可以满足不同场景下的编辑需求。 Vue wangeditor使用也非常简单,只需要在Vue项目中引入相应的组件和依赖,然后进行相关配置即可。同时该编辑器也提供了丰富的API接口,可以方便地对编辑器进行扩展和定制。 总的来说,Vue wangeditor是一款非常实用的富文本编辑器,并且基于Vue.js的优秀框架,具有良好的兼容性和易用性,非常适合在Web应用开发中广泛使用,特别是在需要富文本编辑的场景下。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值