首先先給大家看下效果圖:
效果圖.png
1、下載依賴:
npm i vue-quill-edito -D
2、引入包文件:
<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'
</script>
3、注冊組件:
<script>
export default {
components: {
quillEditor
},
}
</script>
4、使用:
<template>
<!-- 富文本展示 -->
<quill-editor>
ref="myQuillEditor"
@change="onEditorChangeHandle" <!-- 內容改變事件 -->
v-model='quillData' <!-- 雙向綁定的值,可以拿到富文本的內容,為 html 字符串-->
:options="editorOption" <!-- 富文本相關配置 -->
@blur="onEditorBlurHandle($event)" <!-- 獲得焦點事件 -->
@focus="onEditorFocusHandle($event)" <!-- 失去焦點事件 -->
@ready="onEditorReadyHandle($event)"> <!-- 富文本初始化完成事件 -->
</quill-editor>
</template>
5、配置參數以及對應的方法:
<script>
// 富文本工具欄配置
const toolbarOptions = [
["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"], // 清除文本格式
["link", "image", "video"] // 鏈接、圖片、視頻
];
export default {
data() {
return {
//富文本內容
quillData:'',
//富文本配置
editorOption:{
placeholder: "請輸入內容", //同 input 的 placeholder 屬性一致
modules:{
toolbar:toolbarOptions //富文本工具欄配置
}
},
},
methods:{
//失去焦點事件
onEditorBlurHandle(){
},
//獲得焦點事件
onEditorFocusHandle(){
},
//內容改變事件
onEditorChangeHandle(){
},
//初始化完成事件
onEditorReadyHandle(){
}
}
}
</script>
歡迎指教~~~