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

[前端] wang 富文本 vue3

官方链接

https://www.npmjs.com/package/@wangeditor-next/editor-for-vue

<template><div style="border: 1px solid #ccc"><Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" /><Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="editorConfig" :mode="mode"@onCreated="handleCreated" /></div><div v-html="valueHtml"></div>
</template><script>
import '@wangeditor/editor/dist/css/style.css'; // 引入 cssimport { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';export default {components: { Editor, Toolbar },setup() {// 编辑器实例,必须用 shallowRefconst editorRef = shallowRef();// 内容 HTMLconst valueHtml = ref('<p>hello</p>');// 模拟 ajax 异步获取内容onMounted(() => {setTimeout(() => {valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';}, 1500);});const toolbarConfig = {excludeKeys: ['uploadImage', // 隐藏图片上传按钮(使用粘贴功能)'group-video'  // 隐藏视频相关功能]};const editorConfig = {placeholder: '请输入内容...',uploadImgShowBase64: true,MENU_CONF: {uploadImage: {server: null,pasteImage: true,base64LimitSize: 5 * 1024 * 1024}}};// 组件销毁时,也及时销毁编辑器onBeforeUnmount(() => {const editor = editorRef.value;if (editor == null) return;editor.destroy();});const handleCreated = (editor) => {editorRef.value = editor; // 记录 editor 实例,重要!};return {editorRef,valueHtml,mode: 'default', // 或 'simple'toolbarConfig,editorConfig,handleCreated,};},
};
</script><style lang="scss" scoped></style>

改造

<template><div style="border: 1px solid #ccc"><Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default" /><Editorstyle="height: 500px; overflow-y: hidden;"v-model="valueHtml":defaultConfig="editorConfig"mode="default"@onCreated="handleCreated"/></div><div v-html="valueHtml"></div>
</template><script>
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';export default {components: { Editor, Toolbar },data() {return {editorRef: null,      // 编辑器实例引用valueHtml: '<p>hello</p>', // 内容toolbarConfig: {      // 工具栏配置excludeKeys: ['uploadImage', 'group-video']},editorConfig: {       // 编辑器配置placeholder: '请输入内容...',uploadImgShowBase64: true,MENU_CONF: {uploadImage: {server: null,pasteImage: true,base64LimitSize: 5 * 1024 * 1024}}}}},mounted() {// 模拟异步数据setTimeout(() => {this.valueHtml = '<p>模拟 Ajax 异步设置内容</p>'}, 1500)},beforeUnmount() {// 组件销毁时销毁编辑器if (this.editorRef) this.editorRef.destroy()},methods: {handleCreated(editor) {this.editorRef = editor // 保存编辑器实例}}
}
</script>

如果设置read only

      // 只读预览编辑器配置previewEditorConfig: {readOnly: true, // 关键:设置为只读模式autoFocus: false,scroll: false,MENU_CONF: {}}

相关文章:

  • PCIe Switch 问题点
  • 背包问题详解
  • 蓝牙AVRCP协议概述
  • (网络文件系统)N
  • ACM模式用Scanner和System.out超时的解决方案和原理
  • FC7300 IO 无法正常输出高低电平问题排查
  • Elasticsearch/OpenSearch 中doc_values的作用
  • 统信操作系统自定义快捷键配置音量调节功能指南
  • 第六章 进阶10 实习生的焦虑
  • AI 编程 “幻觉” 风险频发?飞算 JavaAI 硬核技术筑牢安全防线
  • 龙虎榜——20250516
  • 香港 GPU 服务器优势及使用场景解析
  • Python开源项目月排行 2025年4月
  • FC7300 Clock介绍
  • LocaleContextResolver实现多语言切换-笔记
  • MySQL8新特性
  • 通过python安装小智语音服务器端
  • window nvidia-smi命令 Failed to initialize NVML: Unknown Error
  • Elabscience 精准识别 CD4+ T 细胞|大鼠源单克隆抗体 GK1.5,适配小鼠样本的流式优选方案
  • 强化学习算法实战:一个例子搞懂sarsa、dqn、ddqn、qac、a2c及其区别
  • 3月中国减持189亿美元美债、持仓规模降至第三,英国升至第二
  • 广西壮族自治区党委常委会:坚决拥护党中央对蓝天立进行审查调查的决定
  • 中国情怀:时代记录与家国镜相|澎湃·镜相第三届非虚构写作大赛征稿启事
  • 四个“从未如此”使巴以加沙战火绵延时间创下历史之最
  • 基金经理调仓引发大金融板块拉升?公募新规落地究竟利好哪些板块
  • 公元1058年:柳永词为什么时好时坏?