Vue3整合WangEditor富文本編輯器的實踐指南
一、為什么選擇 WangEditor
作為國內(nèi)流行的開源富文本編輯器,WangEditor 具有以下優(yōu)勢:
- 輕量高效:壓縮后僅 1.5MB,遠(yuǎn)小于其他同類產(chǎn)品
- 開箱即用:提供 Vue/React 官方封裝組件
- 擴(kuò)展性強(qiáng):支持自定義菜單、異步圖片上傳等
- 安全可靠:內(nèi)置 XSS 過濾機(jī)制
二、快速集成到 Vue 項目
1. 安裝依賴
npm install @wangeditor/editor @wangeditor/editor-for-vue
2. 基礎(chǔ)組件封裝
<template> <div class="editor-wrapper"> <!-- 工具欄 --> <Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" /> <!-- 編輯器 --> <Editor v-model="valueHtml" :defaultConfig="editorConfig" @onCreated="handleCreated" /> </div> </template> <script setup> import { Editor, Toolbar } from '@wangeditor/editor-for-vue' // 編輯器實例(必須使用 shallowRef) const editorRef = shallowRef() // 內(nèi)容數(shù)據(jù)綁定 const valueHtml = ref('<p>初始內(nèi)容</p>') </script>
三、核心功能實現(xiàn)
1. 圖片上傳集成
editorConfig.MENU_CONF['uploadImage'] = { allowedFileTypes: ['image/*'], customUpload: async (file, insertFn) => { try { const ossUrl = await uploadToOSS(file) // 對接云存儲 insertFn(ossUrl, '圖片描述') // 插入編輯器 } catch (error) { console.error('上傳失敗:', error) } } }
注:推薦結(jié)合七牛云/阿里云 OSS 實現(xiàn)文件存儲
2. 數(shù)據(jù)雙向綁定
// 父組件傳值 watch(() => props.modelValue, (newVal) => { if (valueHtml.value !== newVal) { valueHtml.value = newVal } }) // 子組件更新 watch(valueHtml, (newVal) => { emit('update:modelValue', newVal) })
3. 工具欄定制
// 隱藏不常用功能 const toolbarConfig = { excludeKeys: [ 'fullScreen', 'codeBlock', 'code' ] }
四、高級實踐技巧
1. 動態(tài)獲取工具欄配置
onMounted(() => { setTimeout(() => { const editor = editorRef.value const toolbar = DomEditor.getToolbar(editor) console.log(toolbar.getConfig().toolbarKeys) }, 1500) })
2. 內(nèi)存泄漏防護(hù)
onBeforeUnmount(() => { const editor = editorRef.value editor?.destroy() // 必須銷毀實例 })
3. 內(nèi)容變化監(jiān)聽
const handleCreated = (editor) => { editorRef.value = editor // 監(jiān)聽編輯器變化 editor.on('change', () => { console.log('內(nèi)容變化:', editor.getHtml()) }) }
五、最佳實踐建議
樣式隔離:通過外層容器限制編輯器寬度
.editor-wrapper { max-width: 1200px; margin: 0 auto; border: 1px solid #eee; }
性能優(yōu)化:
- 使用 shallowRef 存儲編輯器實例
- 避免頻繁操作 DOM
安全策略:
啟用 XSS 過濾
editorConfig = { MENU_CONF: { uploadImage: { customAlert: (s) => { alert(s) } } } }
使用組件:
<RichTextEditor v-model="content" />
六、總結(jié)
通過本文的實踐,我們已經(jīng)實現(xiàn)了:
? 編輯器的完整集成
? 云端圖片上傳
? 數(shù)據(jù)雙向綁定
? 工具欄定制
? 內(nèi)存安全防護(hù)
預(yù)覽功能:Vue3實現(xiàn)HTML內(nèi)容預(yù)覽功能
官方資源:
到此這篇關(guān)于Vue3整合WangEditor富文本編輯器的實踐指南的文章就介紹到這了,更多相關(guān)Vue3整合WangEditor富文本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue頁面監(jiān)聽是否置為后臺或可見狀態(tài)問題
這篇文章主要介紹了vue頁面監(jiān)聽是否置為后臺或可見狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vuex學(xué)習(xí)進(jìn)階篇之getters的使用教程
getters用于獲取state里的數(shù)據(jù),它類似于計算屬性,如果要獲取的數(shù)據(jù)并沒有發(fā)生變化的話,就會返回緩存的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于vuex學(xué)習(xí)進(jìn)階篇之getters的使用教程,需要的朋友可以參考下2022-10-10Vue向后端傳數(shù)據(jù)后端接收為null問題及解決
這篇文章主要介紹了Vue向后端傳數(shù)據(jù)后端接收為null問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Vue前端導(dǎo)出Excel文件的詳細(xì)實現(xiàn)方案
在開發(fā)后臺管理系統(tǒng)的時候,很多地方都要用到導(dǎo)出excel表格,比如將table中的數(shù)據(jù)導(dǎo)出到本地,下面這篇文章主要給大家介紹了關(guān)于Vue導(dǎo)出Excel文件的相關(guān)資料,需要的朋友可以參考下2021-09-09