vue2 使用 wangeditor

1:安装
npm install @wangeditor/editor @wangeditor/editor-for-vue --save

对应的两个版本^5.1.23   ^1.0.2

yarn add @wangeditor/editor @wangeditor/editor-for-vue

2:页面引入和使用

<templete>
<div>
<!-- 隐藏的文件上传  用于自定义富文本上传文件-->
<el-upload
        v-if="1==2"
          class="upload-demo1"
          action="111"
          :show-file-list="false"
          :before-upload="handleChange1"
        >
          <el-button  size="mini" type="primary"
            >上传附件</el-button
          >
        </el-upload>

<div style="border: 1px solid #ccc;">
          <!-- 工具栏 -->
          <Toolbar
              style="border-bottom: 1px solid #ccc"
              :editor="editor"
              :defaultConfig="toolbarConfig"
          />
          <!-- 编辑器 -->
          <Editor
              style="height: 500px; overflow-y: hidden;"
              :defaultConfig="editorConfig"
              v-model="html"
              @onCreated="onCreated"
          />
      </div>
</div>
</templete>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
    components: {Editor, Toolbar},
    data(){
        // 富文本
      editor: null,
    //富文本内容,传值给后端的会特别大,后端要有一个特别能装的类型
      html: '',
      toolbarConfig: {
        //隐藏视频上传
        excludeKeys: [
            'insertVideo',
            'uploadVideo',
            'group-video'
        ]
      },
      editorConfig: {
           placeholder: '请输入内容...',
           MENU_CONF: {	
             
              async customBrowseAndUpload(resultFiles, insertImgFn) { 
              // 自定义上传按钮,点击上传的时候,触发隐藏按钮
                  document.querySelector('.upload-demo1 .el-upload__input').click()
               },
             },
      },
    },

    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
        },
        // 销毁富文本编辑器
        destroyEdit() { 
           const editor = this.editor
           if (editor == null) return
           editor.destroy() // 组件销毁时,及时销毁 editor ,重要!!!
        },
        handleChange1(file) {     
        // 读取文件并转换为Base64  后端需要一个特别能装的类型,图片是base64的无需调取后端接口
            const reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = () => {
            const base64String = reader.result;              
            const img = `<img src="${base64String}" alt="${file.name}"/>`;
            // 上传成功后把图片插入内容
            this.editor.dangerouslyInsertHtml(img);          
            };
        },
        
    },
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值