富文本编辑器-WangEditor
vue2 + WangEditor
引入WangEditor
1 | npm install wangeditor --save |
富文本编辑器组件:WangEditor.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <template> <!-- 富文本编辑器组件 --> <div> <div ref = "editor" style= "text-align: left;" ></div> </div> </template> <script> import E from 'wangeditor' ; export default { name: 'WangEditor' , data() { return { editor: null , // WangEditor 实例 }; }, props: { content: { type: String, default : '' } }, watch: { // 当父组件传入的 content 变化时,更新编辑器内容 content(newContent) { if ( this .editor && newContent !== this .editor.txt.html()) { this .editor.txt.html(newContent); } } }, mounted() { // 初始化 WangEditor this .editor = new E( this .$refs.editor); this .editor.config.onchange = () => { // 编辑器内容变化时,触发 input 事件传递给父组件 this .$emit( 'input' , this .editor.txt.html()); }; // 配置菜单和其他设置 this .editor.config.menus = [ 'head' , // 标题 'bold' , // 粗体 'italic' , // 斜体 'underline' , // 下划线 'image' , // 图片 'link' , // 链接 'list' , // 列表 'undo' , // 撤销 'redo' , // 重做 ]; this .editor.config.zIndex = 1000; // 创建编辑器 this .editor.create(); // 设置初始内容 if ( this .content) { this .editor.txt.html( this .content); } }, beforeDestroy() { // 销毁编辑器实例,释放资源 if ( this .editor) { this .editor.destroy(); } } }; </script> <style scoped> </style> |
使用组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <template> <div> <!-- 发布博客 --> <el-main> <el-card shadow= "never" style= "height: 870px;" > <br> <WangEditor v-model= "editorContent" /> <el-divider>↓ 内容预览 ↓</el-divider> <el-card shadow= "never" style= "height: 400px;" > <div> <div v-html= "editorContent" ></div> </div> </el-card> </el-card> </el-main> </div> </template> <script> import WangEditor from '../../WangEditor.vue' export default { name: 'blogs' , components: { WangEditor }, data() { return { editorContent: '<p>快来发布你的博客吧!</p>' // 初始化编辑器内容 } } } </script> <style scoped> .el-main { background-color: white; color: #333; text-align: center; line-height: 20px; } </style> |
参考————
https://blog.csdn.net/2202_75337835/article/details/141932447
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)