vue實(shí)現(xiàn)富文本編輯器詳細(xì)過程
vue實(shí)現(xiàn)富文本
1、引入插件
npm install vue-quill-editor --save
2、在封裝的組件中引入并注冊(cè)
<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";
export default {
components: { quillEditor },
data() {
return {
content: "", // 文本內(nèi)容
};
},
methods: {
// 失去焦點(diǎn)事件
onEditorBlur() {},
// 獲得焦點(diǎn)事件
onEditorFocus() {},
// 準(zhǔn)備編輯器
onEditorReady() {},
// 內(nèi)容改變事件
onEditorChange() {},
},
watch: {
// 監(jiān)聽文本變化內(nèi)容
content() {
console.log(this.content);
},
},
};
</script>
3、使用注冊(cè)的組件內(nèi)容
<template>
<!-- 富文本 -->
<quill-editor
ref="myQuillEditor"
v-model="content"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@ready="onEditorReady($event)"
@change="onEditorChange($event)"
/>
</template>
4、根據(jù)自身使用情況選擇全局注冊(cè)還是某一頁面注冊(cè)使用,顯示效果如下:

5、回顯顯示,使用v-html
<div v-html="msg" class="ql-editor"></div>
注意:class="ql-editor" 是為了將文本樣式與富文本框輸入的樣式保持一致(也可自己寫樣式)
其他(功能優(yōu)化)
1、輸入框提示文本
editorOption: {
placeholder: "請(qǐng)輸入需要編寫的內(nèi)容...",
},
2、內(nèi)容控件漢化
提示:將代碼樣式復(fù)制到對(duì)應(yīng)組件中即刻
<style>
.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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題6";
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
content: "標(biāo)準(zhǔn)字體";
}
.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: "等寬字體";
}
</style>
3、顯示效果如下:

4、編輯圖片
4.1、安裝依賴包,包含編輯器包,拖拽包,縮放包
npm i quill-image-drop-module -S // 拖拽插件 npm i quill-image-resize-module -S // 放大縮小插件
4.2、組件里引入使用
import { ImageDrop } from "quill-image-drop-module"; // 圖片拖動(dòng)組件引用
import ImageResize from "quill-image-resize-module"; // 圖片縮放組件引用
Quill.register("modules/imageDrop", ImageDrop); // 注冊(cè)
Quill.register("modules/imageResize", ImageResize); // 注冊(cè)
4.3、在data中配置`
editorOption: {
placeholder: "請(qǐng)輸入需要編寫的內(nèi)容...",
modules: {
imageDrop: true, //圖片拖拽
imageResize: {
//放大縮小
displayStyles: {
backgroundColor: "black",
border: "none",
color: "white",
},
modules: ["Resize", "DisplaySize", "Toolbar"],
},
// 需要重置工具,不然富文本工具上的功能會(huì)缺失
toolbar: [
["bold", "italic", "underline", "strike"], // 加粗 斜體 下劃線 刪除線
["blockquote", "code-block"], // 引用 代碼塊
[{ header: 1 }, { header: 2 }], // 1、2 級(jí)標(biāo)題
[{ list: "ordered" }, { list: "bullet" }], // 有序、無序列表
[{ script: "sub" }, { script: "super" }], // 上標(biāo)/下標(biāo)
[{ indent: "-1" }, { indent: "+1" }], // 縮進(jìn)
[{ direction: "rtl" }], // 文本方向
[
{
size: [
"12",
"14",
"16",
"18",
"20",
"22",
"24",
"28",
"32",
"36",
],
},
], // 字體大小
[{ header: [1, 2, 3, 4, 5, 6] }], // 標(biāo)題
[{ color: [] }, { background: [] }], // 字體顏色、字體背景顏色
// [{ font: ['songti'] }], // 字體種類
[{ align: [] }], // 對(duì)齊方式
["clean"], // 清除文本格式
["image", "video"], // 鏈接、圖片、視頻
],
},
},
4.4、將在webpack中對(duì)插件進(jìn)行配置
提示:找到文件中vue.config.js進(jìn)行配置
const webpack = require('webpack') // 引入webpack
module.exports = {
// 在vue.config.js中configureWebpack中配置
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
]
}
}
注意:配置完成后需要重啟服務(wù)
完整組件代碼
<template>
<!-- 富文本 -->
<div>
<quill-editor
ref="myQuillEditor"
v-model="textContent.content"
:options="editorOption"
@blur="onEditorBlur()"
@focus="onEditorFocus()"
@ready="onEditorReady()"
@change="onEditorChange()"
/>
<el-button
class="button"
size="small"
type="primary"
@click="handleSendData"
>保存/發(fā)布</el-button
>
</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 { ImageDrop } from "quill-image-drop-module"; // 圖片拖動(dòng)組件引用
import ImageResize from "quill-image-resize-module"; // 圖片縮放組件引用
Quill.register("modules/imageDrop", ImageDrop); // 注冊(cè)
Quill.register("modules/imageResize", ImageResize); // 注冊(cè)
export default {
components: { quillEditor },
data() {
return {
textContent: {
content: "",
textShow: false,
},
editorOption: {
placeholder: "請(qǐng)輸入需要編寫的內(nèi)容...",
modules: {
imageDrop: true, //圖片拖拽
imageResize: {
//放大縮小
displayStyles: {
backgroundColor: "black",
border: "none",
color: "white",
},
modules: ["Resize", "DisplaySize", "Toolbar"],
},
toolbar: [
["bold", "italic", "underline", "strike"], // 加粗 斜體 下劃線 刪除線
["blockquote", "code-block"], // 引用 代碼塊
[{ header: 1 }, { header: 2 }], // 1、2 級(jí)標(biāo)題
[{ list: "ordered" }, { list: "bullet" }], // 有序、無序列表
[{ script: "sub" }, { script: "super" }], // 上標(biāo)/下標(biāo)
[{ indent: "-1" }, { indent: "+1" }], // 縮進(jìn)
[{ direction: "rtl" }], // 文本方向
[
{
size: [
"12",
"14",
"16",
"18",
"20",
"22",
"24",
"28",
"32",
"36",
],
},
], // 字體大小
[{ header: [1, 2, 3, 4, 5, 6] }], // 標(biāo)題
[{ color: [] }, { background: [] }], // 字體顏色、字體背景顏色
// [{ font: ['songti'] }], // 字體種類
[{ align: [] }], // 對(duì)齊方式
["clean"], // 清除文本格式
["image", "video"], // 鏈接、圖片、視頻
],
},
},
};
},
methods: {
// 失去焦點(diǎn)事件
onEditorBlur() {},
// 獲得焦點(diǎn)事件
onEditorFocus() {},
// 準(zhǔn)備編輯器
onEditorReady() {},
// 內(nèi)容改變事件
onEditorChange() {},
//保存按鈕點(diǎn)擊事件
handleSendData() {
this.$emit("sendContentData", this.textContent);
},
// 獲取已有的文本內(nèi)容
getContent(e) {
this.textContent.content = e;
},
},
// watch: {
// // 監(jiān)聽文本變化內(nèi)容
// content() {
// console.log(this.content);
// },
// },
};
</script>
<style>
/* 富文本框漢化 */
.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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題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: "標(biāo)題6";
}
.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
content: "標(biāo)準(zhǔn)字體";
}
.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: "等寬字體";
}
/* 確認(rèn)按鈕 */
.button {
margin-top: 10px;
}
</style>
鏈接: 官方鏈接
總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)富文本編輯器的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)富文本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue雙向數(shù)據(jù)綁定原理分析、vue2和vue3原理的不同點(diǎn)
這篇文章主要介紹了vue雙向數(shù)據(jù)綁定原理分析、vue2和vue3原理的不同點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue實(shí)現(xiàn)本地存儲(chǔ)添加刪除修改功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)本地存儲(chǔ)添加刪除修改功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Vue項(xiàng)目中CSS?Modules和Scoped?CSS的介紹與區(qū)別
在vue中我們有兩種方式可以定義css作用域,一種是scoped,另一種就是css modules,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中CSS?Modules和Scoped?CSS的相關(guān)資料,需要的朋友可以參考下2022-03-03
如何解決this.$refs.form.validate()不執(zhí)行的問題
這篇文章主要介紹了如何解決this.$refs.form.validate()不執(zhí)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Vue 2.0在IE11中打開項(xiàng)目頁面空白的問題解決
這篇文章主要給大家介紹了關(guān)于Vue 2.0在ie 11中打開項(xiàng)目頁面空白問題的解決方法,文中詳細(xì)分析出現(xiàn)該問題的原因,并給出了詳細(xì)的解決方法,需要的朋友可以參考借鑒,下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07

