vue3使用wangeditor封裝和自定義上傳文件官方教程
更新時間:2023年06月25日 17:35:16 作者:張旭超
這篇文章主要為大家介紹了vue3使用wangeditor封裝和自定義上傳文件的官方教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>
1、基本配置
官方教程 https://www.wangeditor.com/
2、封裝組件
/**
initValue: 父組件傳遞過來的富文本框初始值,這里會實時監(jiān)聽,更新初始值,放置在彈窗中使用,沒有鉤子函數(shù)的更新。
getEditorContent() 方法,父組件通過這個方法獲取富文本編輯器的內(nèi)容,包括數(shù)組格式的和html格式的內(nèi)容
*/
<template>
<div v-html="valueHtml"></div>
<div style="border: 1px solid #ccc">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editorRef"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
style="min-height: 100px; overflow-y: hidden; text-align: left;"
v-model="valueHtml"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="handleCreated"
@onChange="handleChange"
/>
</div>
</template>
<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted, nextTick, watch } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import axios from 'axios'
// 初始值
const props = defineProps({
initValue: String
})
const emits = defineEmits(['getEditorContent'])
// const emits = defineEmits([''])
let mode = ref('default')
// 編輯器實例,必須用 shallowRef
const editorRef = shallowRef()
// 內(nèi)容 HTML
const valueHtml = ref('')
// 模擬 ajax 異步獲取內(nèi)容
onMounted(() => {
nextTick(() => { // 界面節(jié)點更新完以后再修改值。
valueHtml.value = props.initValue
})
})
// 工具欄配置
const toolbarConfig = {
toolbarKeys: [
// 菜單 key
'headerSelect',
'bold', // 加粗
'italic', // 斜體
'through', // 刪除線
'underline', // 下劃線
'bulletedList', // 無序列表
'numberedList', // 有序列表
'color', // 文字顏色
'insertLink', // 插入鏈接
'fontSize', // 字體大小
'lineHeight', // 行高
'uploadImage', // 上傳圖片
'delIndent', // 縮進
'indent', // 增進
'deleteImage',//刪除圖片
'divider', // 分割線
'insertTable', // 插入表格
'justifyCenter', // 居中對齊
'justifyJustify', // 兩端對齊
'justifyLeft', // 左對齊
'justifyRight', // 右對齊
'undo', // 撤銷
'redo', // 重做
'clearStyle', // 清除格式
'fullScreen' // 全屏
]
}
const editorConfig = {
placeholder: '請輸入內(nèi)容...', // 配置默認(rèn)提示
// readOnly: true,
MENU_CONF: { // 配置上傳服務(wù)器地址
uploadImage: {
// 小于該值就插入 base64 格式(而不上傳),默認(rèn)為 0
base64LimitSize: 5 * 1024, // 5kb
// 單個文件的最大體積限制,默認(rèn)為 2M
// maxFileSize: 1 * 1024 * 1024, // 1M
// // 最多可上傳幾個文件,默認(rèn)為 100
// maxNumberOfFiles: 5,
// 選擇文件時的類型限制,默認(rèn)為 ['image/*'] 。如不想限制,則設(shè)置為 []
allowedFileTypes: ['image/*'],
// 自定義上傳
async customUpload(file, insertFn) { // 文件上傳
const formData = new FormData();
formData.set('file', file)
// 這里根據(jù)自己項目封裝情況,設(shè)置上傳地址
axios.defaults.headers.common['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjVhYzc3MDQxLTE3NGItNDEwZC1hOTJlLTVkYzU0YmRhMjllNiIsInVzZXJuYW1lIjoiYWRtaW4ifQ.GhdthjLmw4NP2WV2owYQS70tacRM-pX7NqI7Mo0_j_hatiqtSrqAr0RJC40osRETiQo_dacZcvNqATLsJHL77A'
let result = await axios.post('https://zxc.test.cn/file/upload', formData)
// 插入到富文本編輯器中,主意這里的三個參數(shù)都是必填的,要不然控制臺報錯:typeError: Cannot read properties of undefined (reading 'replace')
insertFn(result.data.data.url, result.data.data.name, result.data.data.name)
}
}
}
}
// 組件銷毀時,也及時銷毀編輯器
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
const handleCreated = (editor) => {
editorRef.value = editor // 創(chuàng)建富文本編輯器
}
const handleChange = (info) => {
// info.children 數(shù)組包含了數(shù)據(jù)類型和內(nèi)容,valueHtml.value內(nèi)容的html格式
emits('getEditorContent', info.children, valueHtml.value)
}
watch(()=>props.initValue, (value) => { // 父組件獲取初始值
valueHtml.value = value
})
</script>3、使用
父組件
<wang-editor :initValue="form.baseInfo.descInfo" @getEditorContent="onEditorChange"></wang-editor>
import WangEditor from '@/components/WangEditor'
const onEditorChange = (arr, html) => {
console.log(arr, html)
}以上就是vue3使用wangeditor封裝和自定義上傳文件官方教程的詳細內(nèi)容,更多關(guān)于vue3 wangeditor封裝上傳文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
前端Vue學(xué)習(xí)之購物車項目實戰(zhàn)記錄
購物車是電商必備的功能,可以讓用戶一次性購買多個商品,下面這篇文章主要給大家介紹了關(guān)于前端Vue學(xué)習(xí)之購物車項目的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
FastApi+Vue+LayUI實現(xiàn)前后端分離的示例代碼
本文主要介紹了FastApi+Vue+LayUI實現(xiàn)前后端分離的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
vue頁面回退或關(guān)閉,發(fā)送請求不中斷問題
這篇文章主要介紹了vue頁面回退或關(guān)閉,發(fā)送請求不中斷問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
解決npm安裝錯誤:No matching version found for&
這篇文章主要介紹了解決npm安裝錯誤:No matching version found for XXX@3.3.6問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
使用 vue 實現(xiàn)滅霸打響指英雄消失的效果附demo
這篇文章主要介紹了使用 vue 實現(xiàn)滅霸打響指英雄消失的效果 demo,需要的朋友可以參考下2019-05-05

