|
@@ -0,0 +1,86 @@
|
|
1
|
+<template>
|
|
2
|
+ <div id="wangeditor">
|
|
3
|
+ <div ref="editorElem" style="text-align:left"/>
|
|
4
|
+ </div>
|
|
5
|
+</template>
|
|
6
|
+
|
|
7
|
+<script>
|
|
8
|
+import E from 'wangeditor'
|
|
9
|
+export default {
|
|
10
|
+ name: 'EditorElem',
|
|
11
|
+ props:['catchData'], // 接收父组件的方法
|
|
12
|
+ data() {
|
|
13
|
+ return {
|
|
14
|
+ editorContent: ''
|
|
15
|
+ }
|
|
16
|
+ },
|
|
17
|
+ mounted() {
|
|
18
|
+ var editor = new E(this.$refs.editorElem) // 创建富文本实例
|
|
19
|
+ editor.customConfig.onchange = (html) => {
|
|
20
|
+ this.editorContent = html
|
|
21
|
+ this.catchData(html) // 把这个html通过catchData的方法传入父组件
|
|
22
|
+ }
|
|
23
|
+ editor.customConfig.uploadImgServer = '你的上传图片的接口'
|
|
24
|
+ editor.customConfig.uploadFileName = '你自定义的文件名'
|
|
25
|
+ editor.customConfig.uploadImgHeaders = {
|
|
26
|
+ 'Accept': '*/*',
|
|
27
|
+ 'Authorization' : 'Bearer ' + token // 头部token
|
|
28
|
+ }
|
|
29
|
+ editor.customConfig.menus = [ // 菜单配置
|
|
30
|
+ 'head',
|
|
31
|
+ 'list', // 列表
|
|
32
|
+ 'justify', // 对齐方式
|
|
33
|
+ 'bold',
|
|
34
|
+ 'fontSize', // 字号
|
|
35
|
+ 'italic',
|
|
36
|
+ 'underline',
|
|
37
|
+ 'image', // 插入图片
|
|
38
|
+ 'foreColor', // 文字颜色
|
|
39
|
+ 'undo', // 撤销
|
|
40
|
+ 'redo' // 重复
|
|
41
|
+ ]
|
|
42
|
+ // 下面是最重要的的方法
|
|
43
|
+ editor.customConfig.uploadImgHooks = {
|
|
44
|
+ before: function(xhr, editor, files) {
|
|
45
|
+ // 图片上传之前触发
|
|
46
|
+ // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files是选择的图片文件
|
|
47
|
+ // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
|
|
48
|
+ // return {
|
|
49
|
+ // prevent: true,
|
|
50
|
+ // msg: '放弃上传'
|
|
51
|
+ // }
|
|
52
|
+ },
|
|
53
|
+ success: function(xhr, editor, result) {
|
|
54
|
+ // 图片上传并返回结果,图片插入成功之后触发
|
|
55
|
+ // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
|
|
56
|
+ this.imgUrl = Object.values(result.data).toString()
|
|
57
|
+ },
|
|
58
|
+ fail: function(xhr, editor, result) {
|
|
59
|
+ // 图片上传并返回结果,但图片插入错误时触发
|
|
60
|
+ // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
|
|
61
|
+ },
|
|
62
|
+ error: function(xhr, editor) {
|
|
63
|
+ // 图片上传出错时触发
|
|
64
|
+ // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
|
|
65
|
+ },
|
|
66
|
+ timeout: function(xhr, editor) {
|
|
67
|
+ // 图片上传超时时触发
|
|
68
|
+ // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
|
|
69
|
+ },
|
|
70
|
+ // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
|
|
71
|
+ // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
|
|
72
|
+ customInsert: function(insertImg, result, editor) {
|
|
73
|
+ // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
|
|
74
|
+ // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
|
|
75
|
+ // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
|
|
76
|
+ const url = Object.values(result.data)// result.data就是服务器返回的图片名字和链接
|
|
77
|
+ JSON.stringify(url)// 在这里转成JSON格式
|
|
78
|
+ insertImg(url)
|
|
79
|
+ // result 必须是一个 JSON 格式字符串!!!否则报错
|
|
80
|
+ }
|
|
81
|
+ }
|
|
82
|
+ editor.create()
|
|
83
|
+ }
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+</script>
|