如何使用Vue-Quill-Editor富文本編輯器?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1、下載Vue-Quill-Editor
npm install vue-quill-editor --save
2、下載quill(Vue-Quill-Editor需要依賴)
npm install quill --save
3、代碼
<template>
<div class="edit_container">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
</div>
</template>
<script>
import { quillEditor } from "vue-quill-editor"; //調(diào)用編輯器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
export default {
components: {
quillEditor
},
data() {
return {
content: `<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>`,
editorOption: {}
}
},
methods: {
onEditorReady(editor) { // 準(zhǔn)備編輯器
},
onEditorBlur(){}, // 失去焦點事件
onEditorFocus(){}, // 獲得焦點事件
onEditorChange(){}, // 內(nèi)容改變事件
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
},
}
}
</script>OK,搞定,簡潔的富文本編輯器就展現(xiàn)在你眼前了,另外附上API。Vue-Quill-Editor
4、存儲及將數(shù)據(jù)庫中的數(shù)據(jù)反顯為HTML字符串
后臺接收到數(shù)據(jù)后會將字符中的標(biāo)簽進行轉(zhuǎn)碼,所以我們要先進行一個解碼的操作讓他變成標(biāo)簽形式的字符串:
例如后臺接收的數(shù)據(jù)如下:"<h2>title<" ,對應(yīng)解碼后就是`<h2>title</h2>`。
//把實體格式字符串轉(zhuǎn)義成HTML格式的字符串
escapeStringHTML(str) {
str = str.replace(/</g,'<');
str = str.replace(/>/g,'>');
return str;
}然后將返回值賦值給對應(yīng)的參數(shù):
<div v-html="str" class="ql-editor">
{{str}}
</div>上面的str就是轉(zhuǎn)碼函數(shù)返回的值,我們要先在data中定義,所以我現(xiàn)在將新增跟展示放在一起,代碼如下:
<template>
<div class="edit_container">
<!-- 新增時輸入 -->
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
<!-- 從數(shù)據(jù)庫讀取展示 -->
<div v-html="str" class="ql-editor">
{{str}}
</div>
</div>
</template>
<script>
import { quillEditor } from "vue-quill-editor"; //調(diào)用編輯器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
export default {
components: {
quillEditor
},
data() {
return {
content: `<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>`,
str: '',
editorOption: {}
}
},
methods: {
onEditorReady(editor) { // 準(zhǔn)備編輯器
},
onEditorBlur(){}, // 失去焦點事件
onEditorFocus(){}, // 獲得焦點事件
onEditorChange(){}, // 內(nèi)容改變事件
// 轉(zhuǎn)碼
escapeStringHTML(str) {
str = str.replace(/</g,'<');
str = str.replace(/>/g,'>');
return str;
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
},
},
mounted() {
let content = ''; // 請求后臺返回的內(nèi)容字符串
this.str = this.escapeStringHTML(content);
}
}
</script>關(guān)于如何使用Vue-Quill-Editor富文本編輯器問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標(biāo)題:如何使用Vue-Quill-Editor富文本編輯器-創(chuàng)新互聯(lián)
文章鏈接:http://www.chinadenli.net/article10/dgegdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化、云服務(wù)器、網(wǎng)站排名、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)