魏熙美 6 yıl önce
ebeveyn
işleme
824a2ac9e1

+ 84
- 69
VUECODE/smart-operate-manage/src/components/Wangeditor/index.vue Dosyayı Görüntüle

@@ -16,81 +16,25 @@ export default {
16 16
       editorObject: ''
17 17
     }
18 18
   },
19
+  watch: {
20
+    content(nw, old) {
21
+      window.console.log('---->', nw, old)
22
+      if (!old && nw) {
23
+        window.console.log('--1111-->', nw)
24
+        this.editorContent = nw
25
+        this.initEditor()
26
+      }
27
+    }
28
+  },
19 29
   computed: {
20 30
     ...mapGetters([
21 31
       'token'
22 32
     ])
23 33
   },
24 34
   mounted() {
25
-    this.editorObject = new E(this.$refs.editorElem) // 创建富文本实例
26
-    var editor = this.editorObject
27
-    editor.customConfig.onchange = (html) => {
28
-      this.editorContent = html
29
-      this.childWangeditorValue() // 把这个html通过catchData的方法传入父组件
30
-      // this.catchData(html) // 把这个html通过catchData的方法传入父组件
31
-    }
32
-    editor.customConfig.zIndex = 100
33
-    editor.customConfig.uploadImgServer = process.env.BASE_API + '/uploadimage'
34
-    editor.customConfig.uploadFileName = 'uploadFiles'
35
-    editor.customConfig.uploadImgHeaders = {
36
-      'Accept': '*/*',
37
-      'Authorization': 'Bearer ' + this.token // 头部token
38
-    }
39
-    editor.customConfig.menus = [ // 菜单配置
40
-      'head',
41
-      'list', // 列表
42
-      'justify', // 对齐方式
43
-      'bold',
44
-      'fontSize', // 字号
45
-      'italic',
46
-      'underline',
47
-      'image', // 插入图片
48
-      'foreColor', // 文字颜色
49
-      'undo', // 撤销
50
-      'redo' // 重复
51
-    ]
52
-    // 下面是最重要的的方法
53
-    editor.customConfig.uploadImgHooks = {
54
-      before: function(xhr, editor, files) {
55
-        // 图片上传之前触发
56
-        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files是选择的图片文件
57
-        // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
58
-        // return {
59
-        //     prevent: true,
60
-        //     msg: '放弃上传'
61
-        // }
62
-      },
63
-      success: function(xhr, editor, result) {
64
-        // 图片上传并返回结果,图片插入成功之后触发
65
-        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
66
-        this.imgUrl = Object.values(result.data).toString()
67
-      },
68
-      fail: function(xhr, editor, result) {
69
-        // 图片上传并返回结果,但图片插入错误时触发
70
-        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
71
-      },
72
-      error: function(xhr, editor) {
73
-        // 图片上传出错时触发
74
-        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
75
-      },
76
-      timeout: function(xhr, editor) {
77
-        // 图片上传超时时触发
78
-        // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
79
-      },
80
-      // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
81
-      // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
82
-      customInsert: function(insertImg, result, editor) {
83
-        // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
84
-        // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
85
-        // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
86
-        const url = Object.values(result.data)// result.data就是服务器返回的图片名字和链接
87
-        JSON.stringify(url)// 在这里转成JSON格式
88
-        insertImg(url)
89
-        // result 必须是一个 JSON 格式字符串!!!否则报错
90
-      }
91
-    }
92
-    editor.create()
93
-    editor.txt.html(this.editorContent)
35
+    this.$nextTick(() => {
36
+      this.initEditor()
37
+    })
94 38
   },
95 39
   methods: {
96 40
     childWangeditorValue() {
@@ -99,6 +43,77 @@ export default {
99 43
     },
100 44
     setWangeditorValue(value) { // 父组件调用此方法传入参数
101 45
       this.editorObject.txt.html(value)
46
+    },
47
+    initEditor() {
48
+      this.editorObject = new E(this.$refs.editorElem) // 创建富文本实例
49
+      var editor = this.editorObject
50
+      editor.customConfig.onchange = (html) => {
51
+        this.editorContent = html
52
+        this.childWangeditorValue() // 把这个html通过catchData的方法传入父组件
53
+        // this.catchData(html) // 把这个html通过catchData的方法传入父组件
54
+      }
55
+      editor.customConfig.zIndex = 100
56
+      editor.customConfig.uploadImgServer = process.env.BASE_API + '/uploadimage'
57
+      editor.customConfig.uploadFileName = 'uploadFiles'
58
+      editor.customConfig.uploadImgHeaders = {
59
+        'Accept': '*/*',
60
+        'Authorization': 'Bearer ' + this.token // 头部token
61
+      }
62
+      editor.customConfig.menus = [ // 菜单配置
63
+        'head',
64
+        'list', // 列表
65
+        'justify', // 对齐方式
66
+        'bold',
67
+        'fontSize', // 字号
68
+        'italic',
69
+        'underline',
70
+        'image', // 插入图片
71
+        'foreColor', // 文字颜色
72
+        'undo', // 撤销
73
+        'redo' // 重复
74
+      ]
75
+      // 下面是最重要的的方法
76
+      editor.customConfig.uploadImgHooks = {
77
+        before: function(xhr, editor, files) {
78
+          // 图片上传之前触发
79
+          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,files是选择的图片文件
80
+          // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
81
+          // return {
82
+          //     prevent: true,
83
+          //     msg: '放弃上传'
84
+          // }
85
+        },
86
+        success: function(xhr, editor, result) {
87
+          // 图片上传并返回结果,图片插入成功之后触发
88
+          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
89
+          this.imgUrl = Object.values(result.data).toString()
90
+        },
91
+        fail: function(xhr, editor, result) {
92
+          // 图片上传并返回结果,但图片插入错误时触发
93
+          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象,result 是服务器端返回的结果
94
+        },
95
+        error: function(xhr, editor) {
96
+          // 图片上传出错时触发
97
+          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
98
+        },
99
+        timeout: function(xhr, editor) {
100
+          // 图片上传超时时触发
101
+          // xhr 是 XMLHttpRequst 对象,editor 是编辑器对象
102
+        },
103
+        // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
104
+        // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
105
+        customInsert: function(insertImg, result, editor) {
106
+          // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
107
+          // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
108
+          // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
109
+          const url = Object.values(result.data)// result.data就是服务器返回的图片名字和链接
110
+          JSON.stringify(url)// 在这里转成JSON格式
111
+          insertImg(url)
112
+          // result 必须是一个 JSON 格式字符串!!!否则报错
113
+        }
114
+      }
115
+      editor.create()
116
+      editor.txt.html(this.editorContent)
102 117
     }
103 118
   }
104 119
 }