1、首先下载Vue-Quill-Editor
npm install vue-quill-editor --save
2、然后还得下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
3、这里因为需要的配置文件过多,我们不在main.js里全局声明,直接封装一个vue组件,存在components下
我这里使用的上传图片为ant design的图片上传
<template>
<div :class="prefixCls">
<!--files 为后端接收参数一样 -->
<a-upload
name="files"
listType="picture-card"
class="image-uploader"
:showUploadList="false"
:action="uploadUrl"
:beforeUpload="beforeUpload"
@change="handleUpload"
>
</a-upload>
<a-spin tip="上传中..." :spinning="spinning">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
@change="onEditorChange($event)">
</quill-editor>
</a-spin>
</div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { uploadUrl } from '@/utils/request'
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{ 'header': 1 }, { 'header': 2 }],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
[{ 'script': 'sub' }, { 'script': 'super' }],
[{ 'indent': '-1' }, { 'indent': '+1' }],
[{ 'direction': 'rtl' }],
[{ 'color': [] }, { 'background': [] }],
[{ 'align': [] }],
['link', 'image', 'video'],
['clean']
]
export default {
name: 'QuillEditor',
components: {
quillEditor
},
props: {
prefixCls: {
type: String,
default: 'ant-editor-quill'
},
value: {
type: String
}
},
data () {
return {
content: null,
editorOption: {
modules: {
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
document.querySelector('.image-uploader input').click()
} else {
this.quill.format('image', false)
}
}
}
}
}
},
uploadUrl: uploadUrl,
spinning: false
}
},
methods: {
beforeUpload (file) {
const isImg = file.type === 'image/jpeg' || file.type === 'image/gif' || file.type === 'image/png' || file.type === 'image/bmp'
if (!isImg) {
this.$message.error('只能上传图片文件!')
return false
}
const isLt3M = file.size / 1024 / 1024 < 3
if (!isLt3M) {
this.$message.error('上传图片必须小于3MB!')
return false
}
this.spinning = true
return true
},
handleUpload (callback) {
const response = callback.file.response
if (response) {
this.spinning = false
const quill = this.$refs.myQuillEditor.quill
if (response.status === 10000) {
this.imageUrl = response.data[0]
const length = quill.getSelection().index
quill.insertEmbed(length, 'image', this.imageUrl)
quill.setSelection(length + 1)
} else {
this.$notification.error({
message: '上传失败',
description: response.message
})
}
}
},
onEditorBlur (quill) {
},
onEditorFocus (quill) {
},
onEditorReady (quill) {
},
onEditorChange ({ quill, html, text }) {
this.$emit('change', html)
}
},
watch: {
value (val) {
this.content = val
}
}
}
</script>
4、其他组件简单使用
<template>
<div>
<vue-quill-editor
:value="articleContent"
ref="quillEditor"
@change="onEditorChange"
style="height: 400px"
></vue-quill-editor>
</div>
</template>
<script>
import VueQuillEditor from '@/components/QuillEditor'
export default {
components: { VueQuillEditor },
data() {
return {
articleContent: '',
}
},
methods: {
onEditorChange(html) {
this.articleContent = html
},
},
}
</script>
5、拓展
如果上传存在图片链接需要在移动端展示,比如(uniapp实现的微信小程序需要展示图片过宽),咱们可以这样简单的对图片进行处理;
articleContent= articleContent.replace(/<img/g,"<img style='max-width:100%;height:auto;'")
|