Vue3中使用富文本編輯器的示例詳解
1. 前言
有不少的前端需求都需要使用到富文本編輯器,但是富文本編輯器百花齊放,每次使用可能都會重新找一個編輯器,所以有了這篇文章. 當(dāng)項目中需要使用到富文本編輯器時,可以直接按照這篇文章的步驟進行,不出意外的話應(yīng)該可以滿足你的絕大多數(shù)需求
2. 項目初始化
以下實例為Vue3項目,可直接集成到你的項目中,所以這里的項目初始化就不做詳細(xì)步驟指引,需要Vue3項目搭建步驟的可以看我前幾篇文章,里面都有
3. 下載富文本編輯器依賴
// 這里使用的是wangEditor富文本編輯器 npm install @wangeditor/editor @wangeditor/editor-for-vue // 或 yane add @wangeditor/editor @wangeditor/editor-for-vue
4. 使用富文本編輯器
<!-- * @Author: wangzhiyu <w19165802736@163.com> * @version: 1.0.0 * @Date: 2024-04-7 11:04:25 * @LastEditTime: 2024-04-08 21:44:20 * @Descripttion: 富文本編輯器組件 --> <template> <div style="border: 1px solid #ccc; width: 50%; margin: 100px auto"> <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default" /> <Editor style="height: 500px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig" mode="default" @onCreated="handleCreated" @customPaste="customPaste" /> </div> <el-button style="margin: 0 auto" @click="getEditorHTML">獲取富文本HTML內(nèi)容</el-button> </template> <script setup> // 富文本編輯器文檔鏈接: https://www.wangeditor.com/v5/getting-started.html // 引入富文本編輯器CSS import '@wangeditor/editor/dist/css/style.css'; import { onBeforeUnmount, ref, shallowRef } from 'vue'; // 導(dǎo)入富文本編輯器的組件 import { Editor, Toolbar } from '@wangeditor/editor-for-vue'; // 編輯器實例,必須用 shallowRef const editorRef = shallowRef(); // 內(nèi)容 HTML const valueHtml = ref('<p>hello <strong>hello world</strong></p>'); const toolbarConfig = {}; const editorConfig = ref({ placeholder: '請輸入內(nèi)容...', MENU_CONF: {} }); // 自定義圖片上傳 editorConfig.value.MENU_CONF['uploadImage'] = { async customUpload(file, insertFn) { console.log('上傳圖片', file); // 將上傳的file圖片轉(zhuǎn)換為base64 const base64 = URL.createObjectURL(file); // 這里的file為上傳的圖片對象,insertFn傳入 insertFn(base64, 'img'); }, }; // 自定義視頻上傳 editorConfig.value.MENU_CONF['uploadVideo'] = { async customUpload(file, insertFn) { console.log('上傳視頻', file); }, }; // 富文本編輯器生成后觸發(fā) const handleCreated = editor => { editorRef.value = editor; // 記錄 editor 實例,重要! console.log(editorConfig.value.MENU_CONF, 'editorConfig.value'); }; // 監(jiān)聽富文本編輯器粘貼行為 const customPaste = (editor, event, callback) => { // 獲取粘貼的純文本 const text = event.clipboardData.getData('text/plain'); if (text) { editor.insertText(text); event.preventDefault(); callback(false); } }; // 獲取富文本html內(nèi)容 const getEditorHTML = () => { console.log(editorRef.value.getHtml()); }; // 組件銷毀時,也及時銷毀編輯器 onBeforeUnmount(() => { const editor = editorRef.value; if (editor == null) return; editor.destroy(); }); </script>
5. 注意點
注意在富文本編輯器中粘貼圖片或者上傳圖片時,會自動觸發(fā)editorConfig.value.MENU_CONF['uploadImage']
回調(diào)函數(shù),需要在這個回調(diào)函數(shù)中處理文件上傳,然后再調(diào)用inserFn
函數(shù)將url
加載到富文本編輯器中
注意在組件銷毀的同時銷毀富文本編輯器
富文本編輯器的實例必須為shallowRef
6. 效果圖
到此這篇關(guān)于Vue3中使用富文本編輯器的示例詳解的文章就介紹到這了,更多相關(guān)Vue3富文本編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element-ui?tree?異步樹實現(xiàn)勾選自動展開、指定展開、指定勾選功能
這篇文章主要介紹了element-ui?tree?異步樹實現(xiàn)勾選自動展開、指定展開、指定勾選,項目中用到了vue的element-ui框架,用到了el-tree組件,由于數(shù)據(jù)量很大,使用了數(shù)據(jù)懶加載模式,即異步樹,需要的朋友可以參考下2022-08-08vue中父組件通過props向子組件傳遞數(shù)據(jù)但子組件接收不到解決辦法
大家都知道可以使用props將父組件的數(shù)據(jù)傳給子組件,下面這篇文章主要給大家介紹了關(guān)于vue中父組件通過props向子組件傳遞數(shù)據(jù)但子組件接收不到的解決辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01vue3?setup語法糖下父組件如何調(diào)用子組件
這篇文章主要介紹了vue3?setup語法糖下父組件如何調(diào)用子組件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題
這篇文章主要介紹了vue在antDesign框架或elementUI框架組件native事件中觸發(fā)2次問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue如何解決watch和methods值執(zhí)行順序問題
這篇文章主要介紹了vue如何解決watch和methods值執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08