前端vue-实现富文本组件

embedded/2024/9/23 21:09:31/

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.ppmy.cn/embedded/115769.html

相关文章

WordPress建站钩子函数及使用

目录 前言&#xff1a; 使用场景&#xff1a; 一、常用的wordpress钩子&#xff08;动作钩子、过滤器钩子&#xff09; 1、动作钩子&#xff08;Action Hooks&#xff09; 2、过滤器钩子&#xff08;Filter Hooks&#xff09; 二、常用钩子示例 1、添加自定义 CSS 和 JS…

Spring为什么要用三级缓存解决循环依赖?

Spring为什么要用三级缓存解决循环依赖&#xff1f; 1. Spring是如何创建一个bean对象2. Spring三级缓存2.1 一级缓存&#xff1a;单例池&#xff0c;经历过完整bean生命&#xff0c;单例Bean对象2.2 二级缓存&#xff1a;提前暴露的Bean2.3 三级缓存&#xff1a;打破循环 3. S…

Qt (17)【Qt 文件操作 读写保存】

阅读导航 引言一、Qt文件概述二、输入输出设备类三、文件读写类四、文件和目录信息类五、自定义“记事本” 引言 在上一篇文章中&#xff0c;我们学习了Qt的事件处理机制&#xff0c;知道了如何响应用户的操作。但应用程序常常还需要处理文件&#xff0c;比如读写数据。所以&a…

【人工智能】Transformers之Pipeline(十九):文生文(text2text-generation)

目录 一、引言 二、文生文&#xff08;text2text-generation&#xff09; 2.1 概述 2.2 Flan-T5: One Model for ALL Tasks 2.3 pipeline参数 2.3.1 pipeline对象实例化参数 2.3.2 pipeline对象使用参数 ​​​​​​​ 2.3.3 pipeline返回参数 ​​​​​​​​​​​…

ubuntu安装emqx

目录 1.预先下载好emqx压缩包 2.使用tar命令解压 3.进入bin目录 5.放开访问端口18083 6.从通过ip地址访问emqx后台 7.默认用户名密码为admin/public 8.登录后台 9.资源包绑定在此博文可自取 1.预先下载好emqx压缩包 2.使用tar命令解压 sudo tar -xzvf emqx-5.0.8-el8-…

如何使用ssm实现大湾区旅游推荐系统的设计与实现+vue

TOC ssm621大湾区旅游推荐系统的设计与实现vue 第1章 绪论 1.1 研究背景意义及内容 1.1.1 研究背景 二十一世纪互联网的出现&#xff0c;改变了几千年以来人们的生活&#xff0c;不仅仅是生活物资的丰富&#xff0c;还有精神层次的丰富。在互联网诞生之前&#xff0c;地域…

【C++】类和对象(一)

类的引入 C语言是面向过程的&#xff0c;关注的是过程&#xff0c;分析出求解问题的步骤&#xff0c;通过函数调用逐步解决问题。 C是基于面向对象的&#xff0c;关注的是对象&#xff0c;将一件事情拆分成不同的对象&#xff0c;靠对象之间的交互完成。 C语言结构体中只能定…

WPF自定义Dialog模板,内容用不同的Page填充

因为审美的不同&#xff0c;就总有些奇奇怪怪的需求&#xff0c;使用框架自带的对话框已经无法满足了&#xff0c;这里记录一下我这边初步设计的对话框。别问为啥要用模板嵌套Page来做对话框&#xff0c;问就是不想写太多的窗体。。。。 模板窗体&#xff08;XAML&#xff09;…