vue+element+富文本编辑器

本文详细介绍如何在Vue项目中全局使用Quill富文本编辑器,包括安装、配置及自定义工具栏,确保图片上传效率并优化存储方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue+element+富文本编辑器

全局使用

1.使用npm安装 vue-quill-editor
npm install vue-quill-editor --save
2.在main.js中引入富文本编辑器
//引用富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
 //引入富文本css
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
//富文本编辑器添加实例
Vue.use(VueQuillEditor, /* { default global options } */)
3.在页面需要的地方放入

v-model 是双向绑定的数据
:options 是要给富文本编辑器的规则,值是个对象

<quill-editor v-model="goodsForm.goods" :options="editorOption">
</quill-editor>

在没有写配置之前,图片也是可以上传的但是他的上传编码是html,这样上传在数据库中非常大,不方便存储,所以我们要改变这种存储方式
不希望这样存储
在这里插入图片描述
如何配置可以参考这个网址

https://github.com/NextBoy/quill-image-extend-module
在这里插入图片描述

这里面的 toolbar对象就是工具栏,对象中的 container:是配置工具栏显示什么东西
在这里插入图片描述
image是个函数,这个函数在你点击图片的时候执行,我们可以在这个函数中设置图片什么时候上传
在这里插入图片描述
如果没有配置container,工具栏就只有默认的几个
在这里插入图片描述接下来我们来配置container:toolbarOptions

const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  ['blockquote', 'code-block'],

  [{ header: 1 }, { header: 2 }], // custom button values
  [{ list: 'ordered' }, { list: 'bullet' }],
  [{ script: 'sub' }, { script: 'super' }], // superscript/subscript
  [{ 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', 'video'],
  ['clean'] // remove formatting button
]

这样就实现我们要的样子了
在这里插入图片描述

### 关于 VueElement-UI 实现富文本编辑 #### 插件推荐 对于在 Vue 项目中集成富文本编辑器,`vue-quill-editor`是一个不错的选择[^1]。此插件不仅易于配置而且具有丰富的功能集。 #### 安装过程 为了开始使用 `vue-quill-editor` ,可以通过 npm 来安装该包到项目里: ```bash npm install vue-quill-editor --save ``` #### 示例代码 下面是一段简单的例子来展示如何在一个基于 VueElement-UI 的应用中引入并使用 Quill 编辑器: ```html <template> <div id="app"> <!-- 富文本编辑器 --> <quill-editor v-model="content"></quill-editor> <!-- 预览区 --> <div class="ql-container ql-snow preview" v-html="content"></div> </div> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor, }, data() { return { content: '', } } } </script> <style scoped> .preview{ margin-top:20px; border:1px solid #ccc; min-height:100px; } </style> ``` 这段代码展示了基本的设置方式以及如何通过双向绑定 (`v-model`) 将输入的内容存储起来,并提供了一个预览区域用于显示渲染后的 HTML 结果。 另外,在某些场景下可能还会涉及到初始化 UEditor 这样的第三方库的情况,比如如下所示的方式可以用来创建一个名为 editor 的实例[^2]: ```javascript this.editor = UE.getEditor('editor', this.config); ``` 不过需要注意的是,UEditor 并不是专门为 Vue 设计的组件,因此如果希望获得更好的开发体验,建议优先考虑像 `vue-quill-editor` 这样更贴近框架特性的选项。 #### 更多功能需求下的解决方案 当面对更加复杂的需求时,还可以参考成熟的管理平台模板如 `vue-element-admin` 。这个开源项目已经实现了许多实用的功能模块,其中包括了对富文本编辑的支持[^3]。这不仅可以节省大量的时间成本,同时也能够借鉴其优秀的架构设计思路。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值