Vue +Element UI +vue-quill-editor 富文本编辑器及插入图片自定义

本文介绍了如何在Vue.js应用中使用Quill富文本编辑器,包括定制工具栏选项、处理图片上传前后事件(如显示loading动画、上传成功和失败处理),以及解决跨域问题的方法。
摘要由CSDN通过智能技术生成

@change=“onEditorChange($event)”

4.js

<script> const toolbarOptions = [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons [{'header': 1}, {'header': 2}], // custom button values [{'list': 'ordered'}, {'list': 'bullet'}], [{'indent': '-1'}, {'indent': '+1'}], // outdent/indent [{'direction': 'rtl'}], // text direction [{'size': ['small', false, 'large', 'huge']}], // custom dropdown [{'header': [1, 2, 3, 4, 5, 6, false]}], [{'color': []}, {'background': []}], // dropdown with defaults from theme [{'font': []}], [{'align': []}], ['link', 'image'], ['clean'] ] export default { data() { return { quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画,刚开始是false,不显示 content: null, editorOption: { placeholder: '', theme: 'snow', // or 'bubble' modules: { toolbar: { container: toolbarOptions, handlers: { 'image': function (value) { if (value) { // 触发input框选择图片文件 document.querySelector('.avatar-uploader input').click() } else { this.quill.format('image', false); } } } } } }, serverUrl: '/manager/common/imgUpload', // 这里写你要上传的图片服务器地址 header: { // token: sessionStorage.token } // 有的图片服务器要求请求头需要有token } }, methods: { onEditorChange({editor, html, text}) {//内容改变事件 console.log("---内容改变事件---") this.content = html console.log(html) }, // 富文本图片上传前 beforeUpload() { // 显示loading动画 this.quillUpdateImg = true }, uploadSuccess(res, file) { // res为图片服务器返回的数据 // 获取富文本组件实例 console.log(res); let quill = this.$refs.myQuillEditor.quill // 如果上传成功 if (res.code == 200 ) { // 获取光标所在位置 let length = quill.getSelection().index; // 插入图片 res.url为服务器返回的图片地址 quill.insertEmbed(length, 'image', res.url) // 调整光标到最后 quill.setSelection(length + 1) } else { this.$message.error('图片插入失败') } // loading动画消失 this.quillUpdateImg = false }, // 富文本图片上传失败 uploadError() { // loading动画消失 this.quillUpdateImg = false this.$message.error('图片插入失败') } } } serverUrl: ‘/manager/common/imgUpload’, // 这里写你要上传的图片服务器地址

header: {

// token: sessionStorage.token

} // 有的图片服务器要求请求头需要有token

}

},

methods: {

onEditorChange({editor, html, text}) {//内容改变事件

console.log(“—内容改变事件—”)

this.content = html

console.log(html)

},

// 富文本图片上传前

beforeUpload() {

// 显示loading动画

this.quillUpdateImg = true

},

uploadSuccess(res, file) {

// res为图片服务器返回的数据

// 获取富文本组件实例

console.log(res);

let quill = this.$refs.myQuillEditor.quill

// 如果上传成功

if (res.code == 200 ) {

// 获取光标所在位置

let length = quill.getSelection().index;

// 插入图片 res.url为服务器返回的图片地址

quill.insertEmbed(length, ‘image’, res.url)

// 调整光标到最后

quill.setSelection(length + 1)

} else {

this.$message.error(‘图片插入失败’)

}

// loading动画消失

this.quillUpdateImg = false

},

// 富文本图片上传失败

uploadError() {

// loading动画消失

this.quillUpdateImg = false

this.$message.error(‘图片插入失败’)

}

}

}

注意:serverUrl :文件上传地址不能直接写全路径,会出现跨域问题报错。需要在conf/index.js 中 进行配置

module.exports = {

dev: {

// Paths

assetsSubDirectory: ‘static’,

assetsPublicPath: ‘/’,

host: ‘localhost’, // can be overwritten by process.env.HOST

port: 8088, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined

autoOpenBrowser: true,

cssSourceMap: true,

proxyTable: {

‘/api’: {

target: ‘http://localhost:18080/’, //设置调用接口域名和端口号别忘了加http

changeOrigin: true,

pathRewrite: {

‘^/api’: ‘/’ //这里理解成用‘/api’代替target里面的地址,组件中我们调接口时直接用/api代替

// 比如我要调用’http://0.0:300/user/add’,直接写‘/api/user/add’即可 代理后地址栏显示/

}

},

‘/manager’: {

target: ‘http://localhost:18081/’,

changeOrigin: true,

pathRewrite: {

‘^/manager’: ‘/’

}

}

最后

你要问前端开发难不难,我就得说计算机领域里常说的一句话,这句话就是『难的不会,会的不难』,对于不熟悉某领域技术的人来说,因为不了解所以产生神秘感,神秘感就会让人感觉很难,也就是『难的不会』;当学会这项技术之后,知道什么什么技术能做到什么做不到,只是做起来花多少时间的问题而已,没啥难的,所以就是『会的不难』。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

我特地针对初学者整理一套前端学习资料

前端路线图

题而已,没啥难的,所以就是『会的不难』。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

我特地针对初学者整理一套前端学习资料

[外链图片转存中…(img-SeykviyJ-1714330280497)]

vue.js的36个技巧

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UIvue-quill-editor富文本编辑器支持插入图片,但是默认的图片上传功能可能不能满足所有需求,需要进行自定义。 首先,在vue-quill-editor的配置中添加`imageHandler`方法,用于处理图片上传: ```javascript <template> <quill-editor v-model="content" :options="editorOption"></quill-editor> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data () { return { content: '', editorOption: { imageHandler: this.imageHandler // 添加imageHandler方法 } } }, methods: { imageHandler () { // 处理图片上传 } } } </script> ``` 然后,可以使用第三方上传组件(如`el-upload`)进行图片上传,上传完成后将图片地址返回给`quill-editor`。可以在`imageHandler`方法中实现该逻辑: ```javascript <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleSuccess" :show-file-list="false" :headers="headers" ref="upload" > <el-button size="small" type="primary">上传图片</el-button> </el-upload> </div> </template> <script> import { quillEditor } from 'vue-quill-editor' import { mapGetters } from 'vuex' export default { components: { quillEditor }, data () { return { content: '', editorOption: { imageHandler: this.imageHandler }, uploadUrl: 'https://www.example.com/upload' // 图片上传地址 } }, computed: { ...mapGetters(['getToken']) // 获取token }, methods: { imageHandler () { const self = this const uploadImg = this.$refs.upload uploadImg.click() uploadImg.$refs.input.onchange = function () { const file = uploadImg.$refs.input.files[0] const formData = new FormData() formData.append('file', file) self.$axios.post(self.uploadUrl, formData, { headers: { 'Authorization': self.getToken // 设置token } }).then(res => { const url = res.data.url // 获取图片地址 const editor = self.$refs.editor.quill // 获取quill对象 const index = (editor.getSelection() || {}).index || editor.getLength() editor.insertEmbed(index, 'image', url) // 插入图片 }).catch(err => { console.log(err) }) } } } } </script> ``` 在这个例子中,使用了`el-upload`组件进行图片上传,上传完成后将图片地址返回给`quill-editor`。在`imageHandler`方法中,通过`this.$refs.editor.quill`获取到了`quill`对象,然后调用`insertEmbed`方法插入图片。 需要注意的是,由于`quill`对象是异步创建的,所以需要在`mounted`生命周期中获取到`quill`对象才能进行图片插入。可以使用`$nextTick`方法来确保获取到了`quill`对象: ```javascript <template> <quill-editor v-model="content" :options="editorOption" ref="editor"></quill-editor> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data () { return { content: '', editorOption: { imageHandler: this.imageHandler } } }, mounted () { this.$nextTick(() => { // 获取quill对象 const editor = this.$refs.editor.quill // 在quill对象中添加图片上传功能 editor.getModule('toolbar').addHandler('image', () => { this.$refs.upload.click() }) }) }, methods: { imageHandler () { // 处理图片上传 } } } </script> ``` 在这个例子中,通过`editor.getModule('toolbar').addHandler`方法,在`quill`对象中添加了一个`image`按钮,点击该按钮时触发了上传图片的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值