vue3使用wangeditor富文本编辑器

 

npm下载

 1 npm i wangeditor -S         

 

在要使用的页面导入

1 import EWangEditor from "wangeditor";
2 import {onMounted, reactive} from "vue";

定义

 

 

 复制下面的代码,放到上面截图的位置即可

 1 setup() {
 2     let data = reactive({});
 3     onMounted(() => {
 4       let editor = new EWangEditor("#editor");
 5       editor.config.uploadImgShowBase64 = true;
 6       editor.config.onchangeTimeout = 400;
 7 
 8       editor.config.customAlert = (err) => {
 9         console.log(err);
10       };
11 
12       editor.customConfig = editor.customConfig
13           ? editor.customConfig
14           : editor.config;
15 
16       editor.customConfig.onchange = (html) => {
17         data.editorContent = html;
18         document.getElementById('in').value = html;
19         console.log(html);
20       };
21       //以下为新增
22       const menuItem = [  //工具栏里有哪些工具
23         'head',  // 标题
24         'bold',  // 粗体
25         'fontSize',  //字号
26         'fontName',  //字体
27         'italic',  // 斜体
28         'underline',  //下划线
29         'strikeThrough',  //删除线
30         'indent',  //缩进
31         'lineHeight',  //行高
32         'foreColor',  //文字颜色
33         'backColor',  //文字背景颜色
34         'link',  //链接,插入一个链接地址,如果填写了描述,则高亮显示描述。若没有,则高亮显示链接
35         'list',  // 序列(有序列表、无序列表)
36         'todo',  //待办事项
37         'justify',  // 对齐方式
38         'quote',  //引用
39         'emoticon',  //表情
40         'image',  //插入图片
41         'video',  //插入视频
42         'table',  //表格
43         'code',  //代码
44         'splitLine',  //分割线
45         'undo',  //撤销
46         'redo' //恢复
47       ];
48 
49       editor.config.menus = [...menuItem]; /* 应用设置好的工具栏 */
50 
51       editor.config.colors = ["#000000", "#eeece0", "#1c487f", "#4d80bf"];  /* 文字颜色、背景色可以选择哪些颜色? */
52 
53       editor.config.fontNames = [ /* 字体工具提供哪些字体? */
54         "黑体",
55         "仿宋",
56         "楷体",
57         "标楷体",
58         "华文仿宋",
59         "华文楷体",
60         "宋体",
61         "微软雅黑",
62       ];
63       editor.config.height = 700;  //你可能发现这个编辑器是没法用样式调高度的?
64 
65 
66       editor.customConfig.customUploadImg = function (files, insert) {
67         // files 是 input 中选中的文件列表
68         // insert 是获取图片 url 后,插入到编辑器的方法
69         // 在这里进行一系列的校验
70         const formData = new FormData();
71         formData.append("file",files[0]);
72         axios.post('http://localhost:8080/itkb/square/article/uploadArticleImage',formData,{
73           'Content-type' : 'multipart/form-data'
74         }).then(res=>{
75           // 上传成功后的处理
76           // 上传代码返回结果之后,将图片插入到编辑器中
77           insert(res.data)
78         },err=>{
79           // 出现错误时的处理
80           console.log('上传图片失败'+err.data)
81         })
82 
83       }
84       //新增至此
85       editor.create();
86     });
87 
88   }

 

使用

<div class="content">
     <p name="editor" id="editor" ref="editor" style="z-index: -1"></p>
     <el-input id="in" type="hidden"></el-input><!--绑定输入--editor输入的绑定到这个里-->
</div>

运行

 

posted @ 2022-08-26 21:45  勤快的懒羊羊  阅读(1484)  评论(0编辑  收藏  举报