vue 使用wangEditor

news/2024/10/23 9:27:20/

1、npm install @wangeditor/core @wangeditor/editor @wangeditor/editor-for-vue

2、封装组件 MyEditor.vue, (这里是通过props content 将展示内容传入)

<template><div><div style="border: 1px solid #ccc; margin-top: 10px"><!-- 工具栏 --><Toolbarstyle="border-bottom: 1px solid #ccc":editor="editor":defaultConfig="toolbarConfig"/><!-- 编辑器 --><Editorstyle="height: 400px; overflow-y: hidden":defaultConfig="editorConfig"v-model="html"@onChange="onChange"@onCreated="onCreated"/></div></div>
</template><script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { DomEditor } from '@wangeditor/editor'export default {name: "MyEditor",components: { Editor, Toolbar },props: {content: {type: String,default: '',},},data() {return {editor: null,html: "",toolbarConfig: {/* 显示哪些菜单,如何排序、分组 */ toolbarKeys: ['headerSelect', '|', 'bold', 'underline', 'italic', 'color', 'bgColor', '|', 'fontSize', 'fontFamily', 'lineHeight', '|', 'bulletedList', 'numberedList', 'todo', '|', 'emotion', 'insertLink', 'insertTable', 'codeBlock', 'divider', ], // excludeKeys: [ ], /* 隐藏哪些菜单 */ },editorConfig: {placeholder: "",// autoFocus: false,readOnly: true, // 只读、不可编辑// 所有的菜单配置,都要在 MENU_CONF 属性下MENU_CONF: {},},};},methods: {onCreated(editor) {this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错},onChange(editor) {// console.log("onChange", editor.getHtml() ); // onChange 时获取编辑器最新内容// const toolbar = DomEditor.getToolbar(editor)// console.log("工具栏配置", toolbar.getConfig().toolbarKeys ); // 工具栏配置},getEditorText() {const editor = this.editor;if (editor == null) return;// console.log(editor.getText()); // 执行 editor API},printEditorHtml() {const editor = this.editor;if (editor == null) return;// console.log(editor.getHtml()); // 执行 editor API},},mounted() {// 模拟 ajax 请求,异步渲染编辑器setTimeout(() => {this.html = this.content;}, 1500);},beforeDestroy() {const editor = this.editor;if (editor == null) return;editor.destroy(); // 组件销毁时,及时销毁 editor ,重要!!!},
};
</script><style src="@wangeditor/editor/dist/css/style.css"></style>

最后在用展示富文本的地方 引用组件就可以了

<My-editor :content='pbbg.gongShiContent'></My-editor>

这里是直接参照的官方示例,示例地址:

vue2-wangEditor-demo - CodeSandboxvue2-wangEditor-demo by wangfupeng1988 using @wangeditor/core, @wangeditor/editor, @wangeditor/editor-for-vue, @wangeditor/plugin-formula, @wangeditor/plugin-mention, core-js, element-ui, katex, vuehttps://codesandbox.io/s/vue2-wangeditor-demo-1rwjms?file=/src/components/MyEditor.vue:0-2180git地址:https://github.com/wangfupeng1988/vue2-wangeditor-demo

官方文档:快速开始 | wangEditor


http://www.ppmy.cn/news/651672.html

相关文章

vue2使用wangEditor

vue2使用wangEditor 前几天一个月薪35k的兄弟&#xff0c;给我推了一个人工智能学习网站&#xff0c;看了一段时间挺有意思的。包括语音识别、机器翻译等从基础到实战都有&#xff0c;很详细&#xff0c;分享给大家。大家及时保存&#xff0c;说不定啥时候就没了。 效果 wang…

wangEditor的一些坑记录

1、在vue中使用wangEditor 网上的资料一堆&#xff0c;但基本都是wangEditor3的运用&#xff0c;wangEditor4只需要把customConfig 改成config即可。 例如&#xff1a;关于在vue项目中使用wangEditor 2、标题样式、斜体样式不生效 感谢文章&#xff1a;https://blog.csdn.ne…

vue2 cli使用wangEditor ( wangEditor安装依赖, wangEditor.vue文件实例, wangEditor工具栏配置的key列表 )

wangEditor官网地址https://www.wangeditor.com/ 安装依赖 yarn add wangeditor/editor # 或者 npm install wangeditor/editor --saveyarn add wangeditor/editor-for-vue # 或者 npm install wangeditor/editor-for-vue --save xxx.vue文件示例 <template><div s…

富文本编辑器wangEditor回显问题

回显时渲染dom报错问题&#xff1a; 富文本编辑器在回显html时偶尔会报以下错误&#xff1a; Error in v-on handler: "Error: Cannot find a descendant at path [1,1,0] in node: {“children”:[],“operations”:[{“type”:“remove_node”,“path”:[0],“node”:…

wangeditor安装

在没有搭建Vue-CLI的情况下安装wangeditor&#xff0c;官网原代码输入命令行后报错。 解决&#xff1a;把去掉&#xff0c;即可安装成功。 即输入npm install wangeditor --save

wangEditor介绍(入门级)

一、什么是富文本编辑器&#xff1f; 相信很多小伙伴都用过富文本编辑器。富文本编辑器&#xff08;Rich Text Editor&#xff0c;RTE&#xff09;是一种可内嵌于浏览器&#xff0c;所见即所得的文本编辑器。可以实现很多功能&#xff0c;如改变字体颜色&#xff0c;插入图片视…

vue使用wangEditor

vue中安装wangEditor npm install wangeditor创建公用组件&#xff1a;在src/components文件夹中创建wangEditor.vue <template lang"html"><div class"wangeditor"><div ref"toolbar" class"toolbar"></div&g…

【学习总结】wangeditor插件使用

之前的项目中就因为这个插件踩坑了&#xff0c;现在正好就用上这个插件了&#xff0c;相比最近使用的fullcalendar插件来说&#xff0c;wangeditor插件好用太多了&#xff08;"wangeditor/editor": "^5.0.0"&#xff09; 首先放上官网地址&#xff1a;点击…