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下的components模板使用及應(yīng)用
這篇文章主要介紹了Vue下的components模板的使用及應(yīng)用,本文通過代碼介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
JavaScript的MVVM庫Vue.js入門學(xué)習(xí)筆記
這篇文章主要介紹了JavaScript的MVVM庫Vue.js入門學(xué)習(xí)筆記,Vue.js是一個新興的js庫,主要用于實現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件,需要的朋友可以參考下2016-05-05
解決vue3中from表單嵌套el-table時填充el-input,v-model不唯一問題
這篇文章主要給大家介紹一下如何解決vue3中from表單嵌套el-table時填充el-input,v-model不唯一問題,文中有相關(guān)的解決方法,通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
在vue項目中使用Jquery-contextmenu插件的步驟講解
今天小編就為大家分享一篇關(guān)于在vue項目中使用Jquery-contextmenu插件的步驟講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
vue中的echarts實現(xiàn)寬度自適應(yīng)的解決方案
這篇文章主要介紹了vue中的echarts實現(xiàn)寬度自適應(yīng),本文給大家分享實現(xiàn)方案,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條功能
這篇文章主要介紹了uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條,需要的朋友可以參考下2019-11-11

