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

WangEditor在Vue前端的应用

1、在Vue项目中安装WangEditor
对于Vue2:
npm install @wangeditor/editor-for-vue --save
或者 yarn add @wangeditor/editor-for-vue
对于Vue3:
npm install @wangeditor/editor-for-vue@next --save
或者 yarn add @wangeditor/editor-for-vue@next
2、将WangEditor封装成组件WangEditor.vue

<template><div><div><button @click="insertText">insert text</button><button @click="printHtml">print html</button><button @click="disable">disable</button></div><div style="border: 1px solid #ccc; margin-top: 10px"><Toolbar:editor="editorRef":defaultConfig="toolbarConfig":mode="mode"style="border-bottom: 1px solid #ccc"/><Editor:defaultConfig="editorConfig":mode="mode"v-model="valueHtml"style="height: 400px; overflow-y: hidden"@onCreated="handleCreated"@onChange="handleChange" @onDestroyed="handleDestroyed" @onFocus="handleFocus" @onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" /> </div> <div style="margin-top: 10px"> <textarea v-model="valueHtml" readonly style="width: 100%; height: 200px; outline: none" ></textarea> </div> </div> </template> <script> import '@wangeditor/editor/dist/css/style.css';import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';import { Editor, Toolbar } from '@wangeditor/editor-for-vue';export default {components: { Editor, Toolbar },setup() {// 编辑器实例,必须用 shallowRef,重要!const editorRef = shallowRef();// 内容 HTMLconst valueHtml = ref('<p>hello</p>');// 模拟 ajax 异步获取内容onMounted(() => {setTimeout(() => {valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';}, 1500);});const toolbarConfig = {};const editorConfig = { placeholder: '请输入内容...' };// 组件销毁时,也及时销毁编辑器,重要!onBeforeUnmount(() => {const editor = editorRef.value;if (editor == null) return;editor.destroy();});// 编辑器回调函数const handleCreated = (editor) => {console.log('created', editor);editorRef.value = editor; // 记录 editor 实例,重要!};const handleChange = (editor) => {console.log('change:', editor.getHtml());};const handleDestroyed = (editor) => {console.log('destroyed', editor);};const handleFocus = (editor) => {console.log('focus', editor);};const handleBlur = (editor) => {console.log('blur', editor);};const customAlert = (info, type) => {alert(`【自定义提示】${type} - ${info}`);};const customPaste = (editor, event, callback) => {console.log('ClipboardEvent 粘贴事件对象', event);// 自定义插入内容editor.insertText('xxx');// 返回值(注意,vue 事件的返回值,不能用 return)callback(false); // 返回 false ,阻止默认粘贴行为// callback(true) // 返回 true ,继续默认的粘贴行为};const insertText = () => {const editor = editorRef.value;if (editor == null) return;editor.insertText('hello world');};const printHtml = () => {const editor = editorRef.value;if (editor == null) return;console.log(editor.getHtml());};const disable = () => {const editor = editorRef.value;if (editor == null) return;editor.disable()};return {editorRef,mode: 'default',valueHtml,toolbarConfig,editorConfig,handleCreated,handleChange,handleDestroyed,handleFocus,handleBlur,customAlert,customPaste,insertText,printHtml,disable};},};</script>

3、调用组件

<template><div><WangEditor></WangEditor></div>
</template><script setup>
import WangEditor from './WangEditor.vue'
</script>

4、运行效果
在这里插入图片描述

http://www.lryc.cn/news/160111.html

相关文章:

  • 初学python的感受
  • SpringSecurity中注解讲解
  • 朔雪流量复制器的前端
  • Anaconda下 Prophet的安装,pystan和fbprophet的版本问题
  • IntelliJ IDEA 简介
  • 20230904 QT客户端服务器搭建聊天室
  • 吴恩达gradio课程:基于开源LLM(large language model)的聊天应用chatbot
  • 【c++】weak_ptrshared_ptr
  • Init函数和main函数
  • 汇川PLC学习Day1:跑马灯程序编写
  • 【已解决】Unknown initial character set index ‘45‘ received from server
  • Java从入门到精通-数组(二)
  • 深入剖析HTTP和HTTPS代理在爬虫中的应用价值
  • SQL语句分类及编写规范
  • 02-zookeeper分布式锁案例
  • 【Spring传播机制底层原理】
  • python通过tkinter制作词云图工具
  • Java-钉钉订阅事件
  • 【DataV/echarts】vue中使用,修改地图和鼠标点击部分的背景色
  • 系统设计类题目汇总四
  • 【C++心愿便利店】No.5---构造函数和析构函数
  • 微软研究院团队获得首届AI药物研发算法大赛总冠军
  • redis实战篇之导入黑马点评项目
  • 【C++】详解红黑树并模拟实现
  • Matlab图像处理-最大类间方差阈值选择法(Otsu)
  • Spring Cloud(Finchley版本)系列教程(三) 服务消费者(Feign)
  • AI图片生成 discord 使用midjourney
  • gitlab 点击Integrations出现500错误
  • 【2023高教社杯】A题 定日镜场的优化设计 问题分析及数学模型
  • rac异常hang死故障分析(sskgxpsnd2)