vue3使用quill富文本編輯器保姆級教程(附踩坑解決)
更新時間:2023年11月30日 11:40:30 作者:墨暖
這篇文章主要給大家介紹了關(guān)于vue3使用quill富文本編輯器保姆級教程的相關(guān)資料,在許多網(wǎng)站和應(yīng)用程序中富文本編輯器是一種常見的工具,它使用戶能夠以直觀的方式創(chuàng)建和編輯文本內(nèi)容,需要的朋友可以參考下
vue3使用quilleditor
本文是封裝成組件使用
先放效果圖
// 安裝插件 npm install @vueup/vue-quill@alpha --save // 局部引入 import { QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css'
先封裝組件,建立如下目錄
全部代碼如下
<template> <div> <!-- 此處注意寫法v-model:content --> <QuillEditor ref="myQuillEditor" theme="snow" v-model:content="content" :options="data.editorOption" contentType="html" @update:content="setValue()" /> <!-- 使用自定義圖片上傳 --> <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" /> </div> </template> <script setup> import { QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css' import { reactive, onMounted, ref, toRaw, watch } from 'vue' const props = defineProps(['value']) const emit = defineEmits(['updateValue']) const content = ref('') const myQuillEditor = ref() // 通過watch監(jiān)聽回顯,筆者這邊使用v-model:content 不能正?;仫@ watch(() => props.value, (val) => { toRaw(myQuillEditor.value).setHTML(val) }, { deep: true }) const fileBtn = ref() const data = reactive({ content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'font': [] }], [{ 'align': [] }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'header': 1 }, { 'header': 2 }], ['image'], [{ 'direction': 'rtl' }], [{ 'color': [] }, { 'background': [] }] ] }, placeholder: '請輸入內(nèi)容...' } }) const imgHandler = (state) => { if (state) { fileBtn.value.click() } } // 拋出更改內(nèi)容,此處避免出錯直接使用文檔提供的getHTML方法 const setValue = () => { const text = toRaw(myQuillEditor.value).getHTML() } const handleUpload = (e) => { const files = Array.prototype.slice.call(e.target.files) // console.log(files, "files") if (!files) { return } const formdata = new FormData() formdata.append('file', files[0]) backsite.uploadFile(formdata) // 此處使用服務(wù)端提供上傳接口 .then(res => { if (res.data.url) { const quill = toRaw(myQuillEditor.value).getQuill() const length = quill.getSelection().index quill.insertEmbed(length, 'image', res.data.url) quill.setSelection(length + 1) } }) } // 初始化編輯器 onMounted(() => { const quill = toRaw(myQuillEditor.value).getQuill() if (myQuillEditor.value) { quill.getModule('toolbar').addHandler('image', imgHandler) } }) </script> <style scoped lang="scss"> // 調(diào)整樣式 :deep(.ql-editor) { min-height: 180px; } :deep(.ql-formats) { height: 21px; line-height: 21px; } </style>
使用
<template> <div class="page"> <Editor :value="emailForm.msg" @updateValue="getMsg" /> </div> </template> <script setup> import Editor from '@/components/Editor/index.vue' const emailForm = reactive({ test_msg: '' }) const getMsg = (val) => { emailForm.msg = val } </script>
本文是第二個頁面使用這個富文本編輯器有可能watch監(jiān)聽中找不到ref,如果不能正常使用可以稍微改裝下在onMounted里賦值然后在setValue里拋出就好
<template> <div> <QuillEditor ref="myQuillEditor" theme="snow" v-model:content="content" :options="data.editorOption" contentType="html" @update:content="setValue()" /> <!-- 使用自定義圖片上傳 --> <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" /> </div> </template> <script setup> import { QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css' import { reactive, onMounted, ref, toRaw, watch } from 'vue' // import { backsite } from '@/api' const props = defineProps(['value']) const emit = defineEmits(['updateValue']) const content = ref('') const myQuillEditor = ref() // watch(() => props.value, (val) => { // console.log(toRaw(myQuillEditor.value)) // toRaw(myQuillEditor.value).setHTML(val) // }, { deep: true }) const fileBtn = ref() const data = reactive({ content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'font': [] }], [{ 'align': [] }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'header': 1 }, { 'header': 2 }], ['image'], [{ 'direction': 'rtl' }], [{ 'color': [] }, { 'background': [] }] ] }, placeholder: '請輸入內(nèi)容...' } }) const imgHandler = (state) => { if (state) { fileBtn.value.click() } } const setValue = () => { const text = toRaw(myQuillEditor.value).getHTML() emit('updateValue', text) } const handleUpload = (e) => { const files = Array.prototype.slice.call(e.target.files) // console.log(files, "files") if (!files) { return } const formdata = new FormData() formdata.append('file', files[0]) // backsite.uploadFile(formdata) // .then(res => { // if (res.data.url) { // const quill = toRaw(myQuillEditor.value).getQuill() // const length = quill.getSelection().index // // 插入圖片,res為服務(wù)器返回的圖片鏈接地址 // quill.insertEmbed(length, 'image', res.data.url) // // 調(diào)整光標(biāo)到最后 // quill.setSelection(length + 1) // } // }) } onMounted(() => { const quill = toRaw(myQuillEditor.value).getQuill() if (myQuillEditor.value) { quill.getModule('toolbar').addHandler('image', imgHandler) } toRaw(myQuillEditor.value).setHTML(props.value) }) </script> <style scoped lang="scss"> :deep(.ql-editor) { min-height: 180px; } :deep(.ql-formats) { height: 21px; line-height: 21px; } </style>
保姆級教程,有問題歡迎提出
總結(jié)
到此這篇關(guān)于vue3使用quill富文本編輯器的文章就介紹到這了,更多相關(guān)vue3使用quill富文本編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條功能
這篇文章主要介紹了uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條,需要的朋友可以參考下2019-11-11vue3+element?Plus實現(xiàn)表格前端分頁完整示例
這篇文章主要給大家介紹了關(guān)于vue3+element?Plus實現(xiàn)表格前端分頁的相關(guān)資料,雖然很多時候后端會把分頁,搜索,排序都做好,但是有些返回數(shù)據(jù)并不多的頁面,或者其他原因不能后端分頁的通常會前端處理,需要的朋友可以參考下2023-08-08使用Vue進(jìn)行數(shù)據(jù)可視化實踐分享
在當(dāng)今的數(shù)據(jù)驅(qū)動時代,數(shù)據(jù)可視化變得越來越重要,它能夠幫助我們更直觀地理解數(shù)據(jù),從而做出更好的決策,在這篇博客中,我們將探索如何使用 Vue 和一些常見的圖表庫(如 Chart.js)來制作漂亮的數(shù)據(jù)可視化效果,需要的朋友可以參考下2024-10-10