博客
关于我
VUE3(二十六)基于wangeditor自定义富文本插件
阅读量:397 次
发布时间:2019-03-05

本文共 3590 字,大约阅读时间需要 11 分钟。

项目中需要使用到wangeditor,这里封装一个自定义组件,便于后期使用。

首先,官方文档:

1:安装

npm i wangeditor --save

2:组件代码

Wangeditor.vue

Wangeditor.ts

import {    onMounted, onBeforeUnmount, ref, watch } from 'vue';// 引入wangeditor组件import WangEditor from 'wangeditor';// 引入代码高亮组件import hljs from 'highlight.js'// 公共状态文件import {     webScoketObject } from "/@/hooks/common"; // 官方文档:https://doc.wangeditor.com/export default {     name: 'Wangeditor',  setup(props: any, content: any) {       // 获取编辑器实例html    const editor = ref();    // 编辑器实例对象    let instance: any = '';    /**     * @name: 监听公共状态栏值变化(控制抽屉显示)     * @author: camellia     * @email: guanchao_gc@qq.com     * @date: 2021-01-10      */    watch(      () => webScoketObject.is_click_send,      () => {           if (webScoketObject.is_click_send)        {             instance.txt.clear();          webScoketObject.is_click_send = false;        }      }    );    /**     * @name: 生命周期函数-----挂载完成     * @author: camellia     * @email: guanchao_gc@qq.com     * @date: 2021-01-19      */    onMounted(() => {         // 编辑器实例对象      instance = new WangEditor(editor.value);      //插入代码语言配置      instance.config.languageType = [        'Bash',        'C',        'C#',        'C++',        'CSS',        'Java',        'JavaScript',        'JSON',        'TypeScript',        'Plain text',        'Html',        'XML',        'SQL',        'Go',        'Kotlin',        'Lua',        'Markdown',        'PHP',        'Python',        'Shell Session',        'Ruby',        'typescript'      ]      // 自定义菜单      instance.config.menus = [        // 'head',        // 'bold', //字体加粗        // 'fontSize',//字号        // 'fontName',//字体        // 'italic',        // 'underline',//下划线        // 'strikeThrough',//删除线        // 'indent',        // 'lineHeight',        'foreColor',        // 'backColor',        'link',        // 'list',//列表        // 'todo',        // 'justify',//对其        // 'quote',// 引用        'emoticon',        'image',        // 'video',//视频        // 'table',//表格        'code',        // 'splitLine',        // 'undo',//撤销        // 'redo',//恢复      ];      // 代码高亮      instance.highlight = hljs;      // 开启本地上传图片(这是后端上传链接)      instance.config.uploadImgServer = '/upload-img';      // 限制上传图片格式      instance.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp'];      // 开启本地上传视频(这是后端上传链接)      instance.config.uploadVideoServer = '/api/upload-video';      // 设置编辑器高度      instance.config.height = 150;      // 设置编辑器页面层级      instance.config.zIndex = 10;      // 设置编辑器placeholder      instance.config.placeholder = '请输入您的文字!';      // 配置编辑器显示颜色      instance.config.colors = [        '#000000',        '#eeece0',        '#1c487f',        '#4d80bf'      ];      // 忽略粘贴内容中的图片      instance.config.pasteIgnoreImg = true;       Object.assign(instance.config, {           // wangeditor 值发生变化的时候        onchange() {             // 将值传递至父组件          content.emit('getWangEditorValue', instance.txt.html());        },        // 上传网络图片回调        linkImgCallback(src:string){             console.log('图片 src ', src)        },        // 上传网络视频回调        onlineVideoCallback(video:string) {             // 自定义回调内容,内容成功插入后会执行该函数          console.log('插入视频内容', video)        }      });      instance.create();    });     /**     * @name: 生命周期函数-----页面卸载之前     * @author: camellia     * @email: guanchao_gc@qq.com     * @date: 2021-01-19      */    onBeforeUnmount(() => {         instance.destroy();      instance = null;    });    return {         editor,    };  },};

3:调用实例:

4:最终效果

在这里插入图片描述

有好的建议,请在下方输入您的建议。

欢迎访问个人博客

欢迎访问小程序:

在这里插入图片描述

转载地址:http://aqhwz.baihongyu.com/

你可能感兴趣的文章
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 源码完全注释(11)ngx_spinlock
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的 SSL 模块安装
查看>>
Nginx 的优化思路,并解析网站防盗链
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 相关介绍(Nginx是什么?能干嘛?)
查看>>
Nginx 知识点一网打尽:动静分离、压缩、缓存、跨域、高可用、性能优化...
查看>>
nginx 禁止以ip形式访问服务器
查看>>
NGINX 端口负载均衡
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>