Vue富文本编辑器的使用vue-quill-deitor
Vue 文本编辑 使用
2023-09-11 14:19:18 时间
目录
vue-quill-deitor
- 版本支持:支持 IE10+ 详情 点击
1. 安装
- 下载(这里使用的版本为3.0.6)
npm install vue-quill-editor --save
- 引入样式并注册(这里有局部注册和全局注册)
- 这里使用了局部注册
import 'quill/dist/quill.core.css'
//这里有两个主题可以选择性的安装,但是必须选择一个主题
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
components: {
quillEditor
}
}
- 继续将官网里里代码进行复制
<template>
<!-- Two-way Data-Binding -->
<quill-editor
ref="myQuillEditor"
v-model="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
/>
<!-- Or manually control the data synchronization -->
<quill-editor
:content="content"
:options="editorOption"
@change="onEditorChange($event)"
/>
</template>
<script>
// 下面这三行代码选择性的引入
import Quill from 'quill'
import someModule from '../yourModulePath/someQuillModule.js'
Quill.register('modules/someModule', someModule)
export default {
data () {
return {
content: '<h2>I am Example</h2>',
editorOption: {
// Some Quill options...
}
}
},
methods: {
onEditorBlur(quill) {
console.log('editor blur!', quill)
},
onEditorFocus(quill) {
console.log('editor focus!', quill)
},
onEditorReady(quill) {
console.log('editor ready!', quill)
},
onEditorChange({ quill, html, text }) {
console.log('editor change!', quill, html, text)
this.content = html
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill
}
},
mounted() {
console.log('this is current quill instance object', this.editor)
}
}
</script>
- 当我们把上面的代码全部复制到组件中,出现的效果如下
他是一个响应式布局,宽度会随着窗口的大小而变化
2. 样式优化
你会发现他是没有高度的,我们可以通过调试
得到相关的类名,给他一个高度。
- 需要注意的是,我们要给的样式,需要放在全局中,因为我们是从
node_modules
引入的,而我们当前的组件的样式之作用与当前组件下的div
- 在
App.vue
的style
标签中写上样式
.ql-container{
height: 220px !important;
}
- 再次观察富文本编辑器,就会发现他编辑区已经具有一定的高度了
3. 事件说明
这里有两种数据绑定的方式 详情点击 API文档
3.1 数据绑定方式一
html
<quill-editor :content="content" :options="editorOption" @change="onEditorChange($event)" />
js
data() {
return {
content: "<h2>I am Example</h2>",
editorOption: {
// Some Quill options...
}
};
},
methods: {
editorChange({ quill, html, text }) {
console.log(html)
this.content = html;
}
},
常用API
API | Value |
---|---|
@change | 当编辑器中的内容变动的时候就会触发事件 |
content | 编辑器的内容区 |
options | 编辑器的一些设置选项 |
3.2 数据绑定方式二
html
<quill-editor
ref="myQuillEditor"
v-model="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
/>
js
methods: {
onEditorBlur(quill) {
},
onEditorFocus(quill) {
},
onEditorReady(quill) {
},
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
}
},
- 使用
v-model
来双向绑定数据 - 可以通过
ref
来获取编辑器的实例
API | Value |
---|---|
@blur | 编辑器失去焦点时触发一次 |
@focus | 编辑器获得焦点时触发一次 |
@ready | 编辑器准备,在初始化加载的时候触发一次 |
4. options配置
具体的配置项请点击
常用配置项
API | Value |
---|---|
placeholder | 编辑器失去焦点时触发一次 |
modules | 包括工具栏的一些其他设置 |
readOnly | 是否为只读 |
theme | 主题的设置 |
5. 工具栏的设置
关于工具栏的设置 详情点击
modules:{
toolbar:[
['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
['blockquote', 'code-block'], //引用,代码块
[{ 'header': 1 }, { 'header': 2 }], // 标题,键值对的形式;1、2表示字体大小
[{ 'list': 'ordered'}, { 'list': 'bullet' }], //列表
[{ 'script': 'sub'}, { 'script': 'super' }], // 上下标
[{ 'indent': '-1'}, { 'indent': '+1' }], // 缩进
[{ 'direction': 'rtl' }], // 文本方向
[{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
[{ 'header': [1, 2, 3, 4, 5, 6, false] }], //几级标题
[{ 'color': [] }, { 'background': [] }], // 字体颜色,字体背景颜色
[{ 'font': [] }], //字体
[{ 'align': [] }], //对齐方式
['clean'], //清除字体样式
['image','video'] //上传图片、上传视频
]
},
theme:'snow'
}
}
6. 汉化
- 将以下代码粘贴到全局样式中
p {
margin: 10px;
}
.edit_container,
.quill-editor {
height: 300px;
}
.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
content: "14px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
content: "10px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
content: "18px";
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
content: "32px";
}
.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
content: "文本";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
content: "标题1";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
content: "标题2";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
content: "标题3";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
content: "标题4";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
content: "标题5";
}
.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
content: "标题6";
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
content: "标准字体";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
content: "衬线字体";
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
content: "等宽字体";
}
7. 图片的拖拽上传
7.1 拖拽上传
图片的拖拽上传需要quill-image-drop-module
插件,详情点击
下载
npm install quill-image-drop-module -S
配置
import { quillEditor } from 'vue-quill-editor'
import * as Quill from 'quill' //引入编辑器
import { ImageDrop } from 'quill-image-drop-module';
Quill.register('modules/imageDrop', ImageDrop);
export default {
name: 'App',
data(){
return{
editorOption:{
modules:{
imageDrop:true,
},
theme:'snow'
}
}
}
将上面所有配置好后,你就会发现可以拖拽图片到固定的地方了
7.2 大小调整
需要下载插件quill-image-resize-module
,详情点击
下载
npm install quill-image-resize-module -S
配置
import Quill from 'quill';
import { ImageResize } from 'quill-image-resize-module';
Quill.register('modules/imageResize', ImageResize);
const quill = new Quill(editor, {
// ...
modules: {
// ...
imageResize: {
// See optional "config" below
}
}
});
当配置完之后,你会发现控制台报错Cannot read property 'imports' of undefined
解决办法
- 打开
vue.config.js
,添加如下代码
const webpack = require('webpack')
module.exports = {
chainWebpack: config => {
config.plugin('provide').use(webpack.ProvidePlugin, [{
'window.Quill': 'quill'
}]);
},
}
相关文章
- vue中const
- (尚033)Vue_案例_slot(组件间的通信4:slot)
- (尚018-第二章2.1)Vue使用vue-cli创建模板项目
- [转] vue自定义组件(通过Vue.use()来使用)即install的使用
- 【Vue】你必须要知道的vue中v-on指令的修饰符
- 【Vue】通过“全局路由守卫”实现路由组件”切换“时的”权限控制“
- Vue - 实现图片裁剪功能,并上传到服务器(内置第三方最优秀的裁剪图片组件,上传到服务器功能)干净整洁无 BUG 的示例源码与超详细的注释,兼容任意浏览器
- Vue - 引入集成 Tinymce 富文本编辑器(详细步骤及运行Demo),在 Vue.js 项目中使用富文本插件详细教程,附带超级详细的注释说明
- Vue - 完美解决小数的四则运算(加减乘除)导致精度丢失问题,提供详细计算示例代码vue数据计算丢失精度
- Vue——详解MVVM模型在vue中的使用
- vue 使用moment
- Vue2.0 搭建Vue脚手架(vue-cli)
- Vue vue-awesome-swiper 的坑
- 关于Vue中main.js,App.vue,index.html之间关系进行总结
- vue-clipboard2
- Vue 国际化之 vue-i18n 的使用
- Mockjs---自定义静态数据---Vue ui视图化的创建项目的笔记
- VUE-015-解决 vue install 引发的 failed Error: not found: python2 问题
- vue学习笔记六:Jquery VS Vue之网络请求明细对照
- vue学习笔记五:Jquery VS Vue之事件监听明细对照
- vue学习笔记四:Jquery VS Vue之元素操作明细对照
- vue引用qrcodejs2生成、下载二维码、复制到剪贴板
- asp.net mvc + vue.js + axios.js
- vue项目中集成腾讯TIM即时通讯
- Vue实战第1章:学习和使用vue-router
- vue后台(三)
- Vue 之 插件 轮播组件 vue-awesome-swiper 的简单使用整理
- Web 前端 之 Vue vue cli 环境的搭建简单整理(简单的一些注意事项)
- 解决:Vue调试工具vue-devtools安装方法——解决下载速度缓慢,安装报错问题
- Vue学习第13天——vue中使用自定义插件
- vue基本使用--refs获取组件或元素