当前位置: 首页 > news >正文

前端vue-实现富文本组件

1.使用wangeditor富文本编辑器
工具网站:https://www.wangeditor.com/v4/
下载安装命令:npm i wangeditor --save
成品如下图:
在这里插入图片描述

组件实现代码

<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;},// 获取htmlgetHtml() {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; // 图片上传maxthis.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>

组件使用方式

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();}
})
http://www.lryc.cn/news/446376.html

相关文章:

  • AUTOSAR汽车电子嵌入式编程精讲300篇-基于CAN总线的气动控制(中)
  • 国内可用ChatGPT-4中文镜像网站整理汇总【持续更新】
  • 前端sm2国密加密时注意
  • LeetCode 面试经典150题 9.回文数
  • select 函数简介
  • python - 在linux上编译py文件为【.so】文件部署项目运行
  • SQL_having_pandas_filter
  • 从软件架构设计角度理解Kafka
  • 什么是中断?
  • 后端(实例)08
  • 【stm32】TIM定时器输出比较-PWM驱动LED呼吸灯/舵机/直流电机
  • 如何使用ssm实现线上旅游体验系统+vue
  • 探索JMeterTools:一个Python驱动的JMeter脚本生成器
  • 【React】组件通信
  • C++核心编程和桌面应用开发 第七天(运算符重载 智能指针)
  • echarts地图的简单使用
  • Qt 项目优化实践方向
  • 常见的15个:自然语言处理(NLP)实战项目
  • CKKS同态加密通用函数近似方法和openFHE实现
  • Webpack 5的新特性:Asset Modules与Dynamic Import
  • 解释python requests包的timeout
  • 蒙语学习快速方法,速记蒙语单词怎么学习更高效!
  • Vue3组件通信13种方法
  • Servlet入门:服务端小程序的初试(自己学习整理的资料)
  • 代码随想录算法训练营第三七天| 动态规划:完全背包理论基础 518.零钱兑换II 377. 组合总和 Ⅳ 322. 零钱兑换
  • [报错解决] 运行MATCHA时需要在线下载Arial.TTF字体,但是无法连接huggingface
  • B-树(不是B减树)原理剖析(1)
  • 【shell脚本8】Shell脚本学习--其他
  • 《深度学习》ResNet残差网络、BN批处理层 结构、原理详解
  • javadoc:jdk 9通过javadoc API读取java源码中的注释信息(comment)