vue-quill-editor上传图
时间: 2023-08-28 07:08:15 浏览: 146
vue-quill-editor实现图片上传功能
5星 · 资源好评率100%
如果要在vue-quill-editor中上传图片,可以使用以下步骤:
1. 安装vue-quill-editor和quill:
```
npm install vue-quill-editor quill --save
```
2. 在项目中引入vue-quill-editor:
```
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor)
```
3. 在组件中使用vue-quill-editor:
```
<template>
<div>
<quill-editor ref="myQuillEditor" :options="editorOption"></quill-editor>
</div>
</template>
<script>
export default {
data () {
return {
editorOption: {
modules: {
toolbar: [
['image', 'code-block']
]
},
placeholder: '请输入内容',
theme: 'snow'
}
}
},
methods: {
insertImage () {
let input = document.createElement('input')
input.setAttribute('type', 'file')
input.click()
// Listen upload local image and save to server
input.onchange = () => {
let file = input.files[0]
// file type is only image.
if (/^image\//.test(file.type)) {
this.uploadImageToServer(file, (imageUrl) => {
let range = this.$refs.myQuillEditor.quill.getSelection()
this.$refs.myQuillEditor.quill.insertEmbed(range.index, 'image', imageUrl)
})
} else {
console.warn('You could only upload images.')
}
}
},
uploadImageToServer (file, callback) {
let formData = new FormData()
formData.append('image', file)
// Your upload image to server api.
axios.post('api/upload/img', formData).then(response => {
if (response.data.success) {
callback(response.data.data.url)
}
})
}
}
}
</script>
```
请注意,这仅是一个示例,需要根据您的具体情况进行调整。如果需要上传图片到服务器,请自行实现上传
阅读全文