Vue使用wangEditor?5富文本編輯器步驟及可能遇到的問(wèn)題
安裝
yarn add @wangeditor/editor # 或者 npm install @wangeditor/editor --save yarn add @wangeditor/editor-for-vue@next # 或者 npm install @wangeditor/editor-for-vue@next --save
官網(wǎng):用于 Vue React | wangEditor
使用
1. 在script中引入css樣式和組件
import '@wangeditor/editor/dist/css/style.css' // 引入 css import { Editor, Toolbar } from '@wangeditor/editor-for-vue' //引入組件
2. 在template中使用組件
<template> <div style="border: 1px solid #ccc"> <!-- 工具欄 --> <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" /> </div> </template>
3. 初始化編輯器
// 編輯器實(shí)例,必須用 shallowRef const editorRef = shallowRef() // 內(nèi)容 HTML const valueHtml = ref('<p>hello</p>') // 工具欄配置 const toolbarConfig = {} // 編輯器配置 const editorConfig = { placeholder: '請(qǐng)輸入內(nèi)容...' } const handleCreated = (editor) => { editorRef.value = editor // 記錄 editor 實(shí)例,重要! } // 組件銷(xiāo)毀時(shí),也及時(shí)銷(xiāo)毀編輯器 onBeforeUnmount(() => { const editor = editorRef.value; if (editor == null) return; editor.destroy(); });
使用效果
4. 工具欄配置
// 工具欄配置 const toolbarConfig = { // 隱藏全屏、視頻 excludeKeys: ['fullScreen', 'group-video'] };
5. 編輯器配置
// 編輯器配置 const editorConfig = { placeholder: '請(qǐng)輸入內(nèi)容...', // 能否編輯 readOnly: true, // 菜單設(shè)置 MENU_CONF: { // 上傳圖片設(shè)置 uploadImage: { // 自定義插入圖片 async customUpload(file: File, insertFn) { try { let formData = new FormData(); formData.append('file', file); //獲取上傳圖片地址 const data = await postFileUpload(formData); // 從 res 中找到 url alt href ,然后插入圖片 insertFn(data, '', data); } catch (error) { console.log(error); } }, }, }, };
使用過(guò)程中的問(wèn)題
1. 獲取工具欄的key值
官網(wǎng)教程獲取工具的key,說(shuō)是toolbar.getConfig().toolbarKeys
但是我通過(guò)打印editorRef.value.getConfig().toolbarKeys
,卻是undefined
最后是通過(guò)檢查html元素查到的key值
2. 全局樣式覆蓋,導(dǎo)致編輯器內(nèi)容樣式失效
項(xiàng)目中有給h1、h2、h3、h4....等元素設(shè)置默認(rèn)font-size
font-weight
等
在項(xiàng)目中單獨(dú)設(shè)置font-size:initial
font-weight:initial
,都不生效
最后是賦值font-size:revert
font-weight:revert
,才解決
revert
表示恢復(fù)為瀏覽器默認(rèn)值
h1 { font-size: 2em; font-weight: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體粗細(xì) */ } h2, h3, h4, h5, h6, p { font-size: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體大小 */ font-weight: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體粗細(xì) */ } ul { list-style-type: disc; }
完整文件
<template> <div class="rizhi"> <div style="padding: 5px"> <a-button style="margin: 0 5px" type="primary" :loading="saveLoading" @click="onSave" v-if="isEdit" > 保存 </a-button> <a-button style="margin: 0 5px" type="primary" @click="onEdit" v-else-if="!isEdit" > 編輯 </a-button> </div> <div class="edit"> <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default" /> <Editor :style="{ height: tabViewContentHeight + 'px' }" v-model="valueHtml" :defaultConfig="editorConfig" mode="default" @onCreated="handleCreated" /> </div> </div> </template> <script lang="tsx"> export default { name: 'xxxx', }; </script> <script lang="tsx" setup> import '@wangeditor/editor/dist/css/style.css'; // 引入 css import { computed, onMounted, onBeforeUnmount, ref, shallowRef } from 'vue'; import { Editor, Toolbar } from '@wangeditor/editor-for-vue'; // 判斷是保存還是編輯 const isEdit = ref(false); // 編輯器實(shí)例,必須用 shallowRef const editorRef = shallowRef(); // 內(nèi)容 HTML const valueHtml = ref(''); // 工具欄配置 const toolbarConfig = { excludeKeys: ['fullScreen', 'group-video'], }; // 編輯器配置 const editorConfig = { placeholder: '請(qǐng)輸入內(nèi)容...', readOnly: true, MENU_CONF: { uploadImage: { // 自定義插入圖片 async customUpload(file: File, insertFn) { try { let formData = new FormData(); formData.append('file', file); const data = '/xx/xx' //await 上傳圖片接口(formData); // 從 res 中找到 url alt href ,然后插入圖片 insertFn(data, '', data); // 上傳圖片后,得保存 imageList1.push(data); } catch (error) { console.log(error); } }, }, }, }; // 初始化編輯器 const handleCreated = (editor) => { editorRef.value = editor; // 記錄 editor 實(shí)例,重要! }; // 保存 const saveLoading = ref(false); // 初始圖片 let imageList1: any = []; // 修改后圖片 let imageList2: any = []; const onSave = async () => { try { saveLoading.value = true; let deleteImageList: any = []; // 保存時(shí)先刪除圖片 imageList2 = editorRef.value.getElemsByType('image').map((item) => item.src); imageList1.forEach((item) => { if (!imageList2.includes(item)) { deleteImageList.push(item); } }); if (deleteImageList.length !== 0) { // 刪除圖片接口 //await 刪除圖片接口(); } // 再保存 //await 保存接口(); //await 刷新接口(); editorRef.value.disable(); isEdit.value = false; messageSucc('保存成功'); } catch (error) {} saveLoading.value = false; }; const onEdit = () => { isEdit.value = true; editorRef.value.enable(); }; //獲取遠(yuǎn)程數(shù)據(jù) const getList = async () => { try { const res = '' //await 獲取數(shù)據(jù)接口(); valueHtml.value = res.updateExplanation; setTimeout(() => { // 獲取初始圖片 imageList1 = editorRef.value.getElemsByType('image').map((item) => item.src); }, 0); } catch (error) {} }; onMounted(async () => { await getList(); }); // 組件銷(xiāo)毀時(shí),也及時(shí)銷(xiāo)毀編輯器 onBeforeUnmount(() => { const editor = editorRef.value; if (editor == null) return; editor.destroy(); }); </script> <style lang="less" scoped> .rizhi { background-color: #fff; display: flex; flex-direction: column; .rizhiHtml { border: 1px solid #ccc; overflow-y: scroll; padding: 5px; flex: 1; } .edit { border: 1px solid #ccc; } } :deep(.w-e-text-container) { h1 { font-size: 2em; font-weight: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體粗細(xì) */ } h2, h3, h4, h5, h6, p { font-size: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體大小 */ font-weight: revert; /* 恢復(fù)為瀏覽器默認(rèn)字體粗細(xì) */ } ul { list-style-type: disc; } } </style>
總結(jié)
到此這篇關(guān)于Vue使用wangEditor 5富文本編輯器步驟及可能遇到的問(wèn)題的文章就介紹到這了,更多相關(guān)Vue使用wangEditor5富文本編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用echarts柱狀圖實(shí)現(xiàn)select下拉刷新數(shù)據(jù)
這篇文章主要介紹了使用echarts柱狀圖實(shí)現(xiàn)select下拉刷新數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10使用Vue做一個(gè)簡(jiǎn)單的todo應(yīng)用的三種方式的示例代碼
這篇文章主要介紹了使用Vue做一個(gè)簡(jiǎn)單的todo應(yīng)用的三種方式的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Element-ui 滾動(dòng)條美化的實(shí)現(xiàn)
本文主要介紹了Element-ui 滾動(dòng)條美化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06簡(jiǎn)化版的vue-router實(shí)現(xiàn)思路詳解
這篇文章主要介紹了簡(jiǎn)化版的vue-router,需要的朋友可以參考下2018-10-10Vue組織架構(gòu)樹(shù)圖組件vue-org-tree的使用解析
這篇文章主要介紹了Vue組織架構(gòu)樹(shù)圖組件vue-org-tree的使用解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue模塊導(dǎo)入報(bào)錯(cuò)問(wèn)題Module not found: Error:[CaseSensi
這篇文章主要介紹了vue模塊導(dǎo)入報(bào)錯(cuò)問(wèn)題Module not found: Error:[CaseSensitivePathsPlugin],具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue cli實(shí)現(xiàn)項(xiàng)目登陸頁(yè)面流程詳解
CLI是一個(gè)全局安裝的npm包,提供了終端里的vue命令。它可以通過(guò)vue create快速搭建一個(gè)新項(xiàng)目,或者直接通過(guò)vue serve構(gòu)建新想法的原型。你也可以通過(guò)vue ui通過(guò)一套圖形化界面管理你的所有項(xiàng)目2022-10-10