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(){}, // 失去焦點事件 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ù)如下:"<h1>title<" ,對應(yīng)解碼后就是`<h1>title</h1>`。
//把實體格式字符串轉(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解VS Code使用之Vue工程配置format代碼格式化
這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03詳解vue3.0 的 Composition API 的一種使用方法
這篇文章主要介紹了vue3.0 的 Composition API 的一種使用方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10