vue整合wangEditor具体实例(包含图片上传和内容回显)

最近在写考试系统,需求是题目能够用富文本编辑。在现在的项目中已经用到Ueditor但是感觉太臃肿了,所以在网上看了几种在线编辑器,最终选择wangEditor(简约、大方适合现在的扁平化风格)。
话不多说直接上代码:
1.在自己项目的目录下使用cnpm下载wangEditor(我用的是淘宝镜像源所以是cnpm)

cnpm install wangeditor

2.新建一个wangEditor.vue的公共组件,在其中引入wangEditor

<template>
    <div class='clearfix'>
        <!-- 富文本编辑框 -->
        <div id="wangEditorElem" style="height:210px;background: #ffffff;"></div>
    </div>
</template>
<script>
import E from 'wangeditor'
export default {
   
   
  name: 'text',
  data () {
   
   
    return {
   
   
      wEditor: '',
      name: ''
    }
  },
  props: {
   
   
    content: {
   
   
      type: String
    }
  }, 
  // 用来回显内容(监控父组件中content变化)
  watch:{
   
   
    content:function (newVal,oldVal) {
   
   
        this.wEditor.txt.html(newVal)
    }
  },
  methods: {
   
   
    //获取编辑器中内容
    getContent
### 配置图片上传接口 在 Vue 项目中使用 WangEditor 实现图片上传功能时,编辑器默认不会直接将图片插入为 Base64 字符串,而是通过配置上传接口,将图片上传到后端服务器。后端服务返回图片存储地址后,WangEditor 会将该地址插入到编辑器内容中显示出来。这种方式更适用于生产环境,避免了 Base64 数据过大影响页面性能。 配置示例如下: ```javascript const editorConfig = { placeholder: &#39;请输入内容...&#39;, MENU_CONF: { uploadImage: { // 指定后端的接口地址 server: &#39;/api/v1/upload/image/avatar2&#39;, // 指定上传的字段名 fieldName: &#39;file&#39; } } }; ``` 上述配置中,`server` 指定了后端图片上传接口的地址,`fieldName` 则是上传时使用的字段名,通常与后端接口接收参数一致[^1]。 ### 处理上传逻辑 WangEditor 的上传配置支持自定义上传逻辑,包括上传前处理、上传成功后的回调等。例如,可以添加请求头、自定义上传参数或处理上传失败的情况: ```javascript const editorConfig = { MENU_CONF: { uploadImage: { server: &#39;/api/upload/image&#39;, fieldName: &#39;file&#39;, // 自定义上传参数 meta: { token: &#39;your_token_here&#39; }, // 请求头 headers: { authorization: &#39;Bearer your_token&#39; }, // 上传前处理 beforeUpload(file) { // 可以在此进行文件校验或修改 return file; }, // 上传成功回调 onSuccess(file, res) { console.log(&#39;上传成功&#39;, res); }, // 上传失败回调 onFailed(file, res) { console.error(&#39;上传失败&#39;, res); }, // 上传错误回调 onError(file, err) { console.error(&#39;上传出错&#39;, err); } } } }; ``` ### 图片粘贴上传支持 WangEditor 支持直接粘贴图片并上传到服务器,包括从 Word、Excel、PowerPoint、PDF 等文档中复制的图片内容。这种功能对于需要频繁从文档中复制图文内容的场景非常实用。通过配置上传接口,粘贴的图片会自动上传到服务器,并将返回的 URL 插入到编辑器中显示。 该功能支持多种文档格式网络图片的导入,适用于教育、政府、医疗等行业中的内容管理系统[^3]。 ### Vue 中的集成实现 在 Vue 项目中集成 WangEditor 并实现图片上传功能时,可以通过 CDN 引入方式加载 WangEditor,并在组件的生命周期钩子中初始化编辑器。例如: ```vue <template> <div> <div id="editor-container" style="width: 100%; height: 400px;"></div> <div v-html="editorContent"></div> </div> </template> <script> export default { data() { return { editorContent: &#39;&#39; }; }, mounted() { const { Editor } = window.wangEditor; const editor = new Editor(&#39;#editor-container&#39;); editor.config.onChange = (newHtml) => { this.editorContent = newHtml; }; editor.config.MENU_CONF = { uploadImage: { server: &#39;/api/upload/image&#39;, fieldName: &#39;file&#39; } }; editor.create(); } }; </script> ``` 在上述代码中,通过 `window.wangEditor` 访问全局对象,并创建编辑器实例。配置 `uploadImage` 指定后端接口地址上传字段名,确保图片上传功能正常工作。 ### 相关问题
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值