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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| <template> <div class="wangeditor"> <div ref="editorElem" class="wangeditor-mach"></div> </div> </template>
<script> import E from 'wangeditor' import { postUpload } from '@/api/upload' import defaultSettings from '@/config/defaultSettings' export default { name: 'Wangeditor', data () { return { editor: {}, editorContent: '', defaultSettings: defaultSettings.settings, path: '', imgServerUrl: process.env.VUE_APP_SERVER_URL } }, watch: { context (val, oldval) { if (val && val !== oldval) { this.handsetHtmlValue(val) } } }, props: { editorData: { type: Function, default: (value) => { return value } }, editorConfig: { type: Object, default: { uploadImgServer: '', uploadFileName: 'file', uploadImgMaxLength: 1, zIndex: 9 } }, context: { type: String, default: '' } }, mounted () { const that = this const editor = new E(this.$refs.editorElem) this.editor = editor editor.customConfig.onchange = (html) => { this.editorData(html) } editor.customConfig.zIndex = this.editorConfig.zIndex || 9 editor.customConfig.uploadFileName = this.editorConfig.uploadFileName editor.customConfig.uploadImgMaxLength = this.editorConfig.uploadImgMaxLength editor.customConfig.uploadImgServer = this.editorConfig.uploadImgServer console.log('图片上传地址:', editor.customConfig.uploadImgServer, this.imgServerUrl)
editor.customConfig.customUploadImg = function (files, insert) {
const fileList = [...files] const formData = new FormData() fileList.forEach(file => { formData.append('file', file) }) postUpload(formData).then(res => { if (res.status !== 'success') { return that.$message.error(res.msg) } console.log('自定义上传方法:', that.imgServerUrl + res.result) insert(that.imgServerUrl + res.result) }).catch(e => { }) } editor.create() }, methods: { handsetHtmlValue (data) { this.editor.txt.html(data) } } } </script>
<style lang="less" scoped> /deep/ .wangeditor-mach .w-e-toolbar { flex-wrap: wrap; } </style>
|