Vue-Quill-Editor富文本編輯器的使用教程
本文為大家分享了Vue Quill Editor富文本編輯器的具體使用方法,供大家參考,具體內(nèi)容如下
先看效果圖:
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(){}, // 失去焦點(diǎn)事件
onEditorFocus(){}, // 獲得焦點(diǎn)事件
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)簽進(jìn)行轉(zhuǎn)碼,所以我們要先進(jìn)行一個(gè)解碼的操作讓他變成標(biāo)簽形式的字符串:
例如后臺接收的數(shù)據(jù)如下:"<h1>title<" ,對應(yīng)解碼后就是`<h1>title</h1>`。
//把實(shí)體格式字符串轉(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">
<!-- 新增時(shí)輸入 -->
<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(){}, // 失去焦點(diǎn)事件
onEditorFocus(){}, // 獲得焦點(diǎn)事件
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue-quill-editor富文本編輯器超詳細(xì)入門使用步驟
- vue使用vue-quill-editor富文本編輯器且將圖片上傳到服務(wù)器的功能
- 淺談vue中使用編輯器vue-quill-editor踩過的坑
- 解決Vue的文本編輯器 vue-quill-editor 小圖標(biāo)樣式排布錯亂問題
- Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義
- 詳解Vue基于vue-quill-editor富文本編輯器使用心得
- Vue項(xiàng)目中quill-editor帶樣式編輯器的使用方法
- Vue?quill-editor?編輯器使用及自定義toobar示例詳解
相關(guān)文章
詳解VS Code使用之Vue工程配置format代碼格式化
這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
詳解vue3.0 的 Composition API 的一種使用方法
這篇文章主要介紹了vue3.0 的 Composition API 的一種使用方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
一個(gè)因@click.stop引發(fā)的bug的解決
這篇文章主要介紹了一個(gè)因@click.stop引發(fā)的bug的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01

