前端vue-实现富文本组件

1.使用wangeditor富文本编辑器

工具网站:https://www.wangeditor.com/v4/

下载安装命令:npm i wangeditor --save

成品如下图:

组件实现代码

javascript 复制代码
<template>
  <div>
    <!-- 富文本编辑器 -->
    <div id="wangeditor">
    </div>
  </div>
</template>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>

import { uploadImage } from '@/api/api'; // 导入图片上传api
import E from 'wangeditor';


export default {
  name: 'richText',
  components: {
  },
  props: {
    defaultDetails: {
      default: '请填写内容',
      type: String,
    },
  },
  watch: {
    htmlContent(val) {
      this.$emit('change', val); // 将改变同步到父组件
      if (this.validateEvent) {
        this.dispatch('ElFormItem', 'el.form.change', [val]);
      }
    }
  },
  data() {
    return {
      editor: null,
      htmlContent: '<p>hello</p>',
      firtherMethod: 'loadingCompleted', // 回调父组件,通知editor已经创建完成
    };
  },
  methods: {
    // 获取text文本
    getText() {
      const text = this.editor.txt.text();
      console.log('text = ', text);
      return text;
    },
    // 获取html
    getHtml() {
      const html = this.editor.txt.html();
      console.log('thml = ', html);
      return html;
    },
    // 图片上传自定义实现
    async uploadImage(files) {
      const file = files[0];
      console.log('Fuedit2-uploadImage file = ', file);
      const res = await uploadImage(obj);
      const path = SOCKET + (res.path || {});
      console.log('完整path = ', path);
      return path;
    },
    // 设置内容
    setHtml(html) {
      this.editor.txt.html(html);// 重新设置编辑器内容
    },
    // 追加内容
    appentHtml(html) {
      this.editor.txt.append(html);// 继续追加内容。
    },
    // 销毁编辑器
    beforeDestroy() {
      // 销毁编辑器
      console.log('销毁前');
      this.editor.destroy()
      console.log('销毁后');
      this.editor = null
    },
    // 清空编辑器内容
    clearText() {
      this.editor.txt.clear();
    },

    createEditor() {
      if(this.editor !== null) {
        return;
      }

      this.editor = new E('#wangeditor');
      // 或者 const editor = new E( document.getElementById('div1') )
      this.editor.config.height = 200; // 设置高度

      // 内容发生改变时回调
      // this.editor.config.onchange = function (html) {
        // this.htmlContent = html;
      // }

      this.editor.config.placeholder = this.defaultDetails; // 自定义初始文字提示

      this.editor.config.focus = false;// 取消初始化时自动聚焦

      this.editor.config.menus = [ // 定义显示哪些菜单和菜单的顺序。
        'head', // 标题
        'bold', // 粗体
        'fontSize', // 字号
        'fontName', // 字体
        'italic', // 斜体
        'underline', // 下划线
        // 'strikeThrough', // 删除线
        // 'indent',
        'lineHeight',
        'foreColor', // 文字颜色
        'backColor', // 背景颜色
        'link', // 插入链接
        'list', // 列表
        // 'todo',
        // 'justify', // 对齐方式
        // 'quote', // 引用
        // 'emoticon', // 表情
        'image', // 插入图片
        // 'table', // 表格
        // 'video', // 插入视频
        // 'code', // 插入代码
        'splitLine',
        'undo', // 撤销
        'redo', // 重复
      ];

      // this.editor.config.uploadImgServer = '/upload-img'; // 配置上传server 接口地址
      this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 图片上传max
      this.editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']; // 图片上传类型
      this.editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片

      this.editor.config.customUploadImg = async function (resultFiles, insertImgFn) { // 自定义图片上传实现
      // resultFiles 是 input 中选中的文件列表;insertImgFn 是获取图片 url 后,插入到编辑器的方法
        const file = resultFiles[0];
        const path = await uploadImage(file);//返回图片地址
        console.log('完整path = ', path);
      // 上传图片,返回结果,将图片插入到编辑器中
        insertImgFn(path);
      }

      // 使用base64格式保存本地图片,不可与uploadImgServer同时使用
      // this.editor.config.uploadImgShowBase64 = true;

      this.editor.create();

      // this.editor.txt.html('<p>用 JS 设置的内容</p>');// 重新设置编辑器内容
      // 第一步,初始化 textarea 的值
      // text1.val(this.editor.txt.html())
      console.log('this.editor = ', this.editor);
      // this.editor.txt.append('<p>追加的内容</p>');// 继续追加内容。

      // 创建完成,回调父组件
      try {
        this.$emit(this.firtherMethod, null);
      } catch (error) {
        console.log('editor 完成,回调父组件失败 error = ', error);
      }
    },
  },
  mounted() {
    this.createEditor();
  },
};
</script>

<style lang="css"  src="">
/* @import '../css/Cnel.css';

/* 使用style属性src引入外部css,仅在当前s组件有效 */
</style>

组件使用方式

javascript 复制代码
RichText: () => import('@/components/RichText.vue'),









<rich-text v-model="details" ref="fueditModule" @loadingCompleted="loadingCompleted"></rich-text>

export default Vue.extend({
  name: 'UpdateText',
  components: {
    RichText: () => import('@/components/RichText.vue'),
  },
  methods: {
  	// 富文本组件加载完成回调
    loadingCompleted() {
      try {
        console.log('editor加载完成,回调父组件');
        // this.details = this.$refs.fueditModule.setHtml('<p><b>招商会详情!!</b></p>');
      } catch (error) {
        console.log('打开弹窗 err =', error);
      }
    },
	// 调用子组件获取富文本内容
	this.details = this.$refs.fueditModule.getHtml();
	// 调用子组件设置富文本内容
	this.$refs.fueditModule.setHtml('<p><b>设置详情!!</b></p>');
	// 调用子组件销毁富文本编辑框
	this.$refs.fueditModule.beforeDestroy();
  }
})
相关推荐
崔庆才丨静觅10 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅10 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅10 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment10 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅11 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊11 小时前
jwt介绍
前端
爱敲代码的小鱼11 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
吹牛不交税11 小时前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore
Cobyte11 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
NEXT0611 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法