Vue3使用富文本框(wangeditor)的方法總結(jié)
更新時間:2024年01月18日 09:45:03 作者:mfxcyh
項目中用到了富文本,選來選去選擇了wangeditor,下面這篇文章主要給大家介紹了關(guān)于Vue3使用富文本框(wangeditor)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
畢業(yè)涉及中使用到了富文本框,所以學(xué)習(xí)使用了wangeditor富文本框,現(xiàn)進(jìn)行總結(jié)

1.安裝
npm install @wangeditor/editor --save npm install @wangeditor/editor-for-vue@next --save
2.配置wangeditor組件(src/components/wangeditor.vue)
<template>
<div style="border: 1px solid #ccc">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editorRef"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
style="min-height: 250px; overflow-y: hidden;"
v-model="valueHtml"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="handleCreated"
/>
</div>
</template>//script標(biāo)簽中引入
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'export default {
components: { Editor, Toolbar },
setup(props,{emit}) {
emits: ['select']
// 編輯器實例,必須用 shallowRef
const editorRef = shallowRef()
// 內(nèi)容 HTML
const valueHtml = ref('')
//配置功能欄
let toolbarConfig = {
toolbarKeys: [
'headerSelect',
'blockquote',
'|',
'bold',
'underline',
'italic',
{
key: 'group-more-style',
title: '更多',
iconSvg:
'<svg viewBox="0 0 1024 1024"><path d="M204.8 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M505.6 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M806.4 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path></svg>',
menuKeys: ['through', 'code', 'sup', 'sub']
},
'color',
'bgColor',
'|',
'fontSize',
{
key: 'group-justify',
title: '對齊',
iconSvg:
'<svg viewBox="0 0 1024 1024"><path d="M768 793.6v102.4H51.2v-102.4h716.8z m204.8-230.4v102.4H51.2v-102.4h921.6z m-204.8-230.4v102.4H51.2v-102.4h716.8zM972.8 102.4v102.4H51.2V102.4h921.6z"></path></svg>',
menuKeys: ['justifyLeft', 'justifyRight', 'justifyCenter', 'justifyJustify']
},
'todo',
'fontFamily',
{
key: 'group-indent',
title: '縮進(jìn)',
iconSvg:
'<svg viewBox="0 0 1024 1024"><path d="M0 64h1024v128H0z m384 192h640v128H384z m0 192h640v128H384z m0 192h640v128H384zM0 832h1024v128H0z m0-128V320l256 192z"></path></svg>',
menuKeys: ['indent', 'delIndent']
},
'|',
'emotion',
'insertLink',
'uploadImage',
'insertTable',
'codeBlock',
'divider',
'clearStyle',
'|',
'undo',
'redo',
]
}
const uploadImageList = ref([])
const saveImageList = ref([])
//上傳本地圖片
function update(file,insertFn) {
let formData = new FormData()
formData.append('file', file)
axios.post('http://localhost:8080/api/file/upload',formData,{
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res => {
if (res.data.code == 0){
const src = 'http://121.37.0.16:9000/public/'+ res.data.data.fileName[0]
insertFn(src, '百度 logo', src)
}
})
}
function getOnInsertedImage(imageNode){
uploadImageList.value.push(imageNode)
}
//編輯器配置
let editorConfig = {
placeholder: '請輸入內(nèi)容...',
// 所有的菜單配置,都要在 MENU_CONF 屬性下
MENU_CONF: {
insertImage:{
onInsertedImage: getOnInsertedImage()
},
// 配置上傳圖片
uploadImage: {
customUpload: update
}
}
}
// 組件銷毀時,也及時銷毀編輯器
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
function copyObject(obj){
return JSON.parse(JSON.stringify(obj));
}
const handleCreated = (editor) => {
editorRef.value = editor // 記錄 editor 實例,重要!
saveImageList.value = editor.getElemsByType('image')
uploadImageList.value = copyObject(saveImageList.value)
console.log('created', editor)
}
watch(() => valueHtml.value,()=>{
//當(dāng)編輯器的內(nèi)容發(fā)生變化時,把值傳給父組件
emit('select', valueHtml.value)
})
const handleChange = (editor) => { console.log('change:', editor.children) }
const handleDestroyed = (editor) => { console.log('destroyed', editor) }
const handleFocus = (editor) => { console.log('focus', editor) }
const handleBlur = (editor) => { console.log('blur', editor) }
const customAlert = (info, type) => { alert(`【自定義提示】${type} - ${info}`) }
const customPaste = (editor, event, callback) => {
console.log('ClipboardEvent 粘貼事件對象', event)
// const html = event.clipboardData.getData('text/html') // 獲取粘貼的 html
// const text = event.clipboardData.getData('text/plain') // 獲取粘貼的純文本
// const rtf = event.clipboardData.getData('text/rtf') // 獲取 rtf 數(shù)據(jù)(如從 word wsp 復(fù)制粘貼)
// 自定義插入內(nèi)容
editor.insertText('xxx')
// 返回 false ,阻止默認(rèn)粘貼行為
event.preventDefault()
callback(false) // 返回值(注意,vue 事件的返回值,不能用 return)
// 返回 true ,繼續(xù)默認(rèn)的粘貼行為
// callback(true)
}
//父組件調(diào)用子組件的方法清空編輯器內(nèi)容
const abc =function (){
valueHtml.value = ''
}
//暴露該方法,defineExpose要引入
defineExpose({
abc
})
return {
editorRef,
valueHtml,
mode: 'default', // 或 'simple'
toolbarConfig,
editorConfig,
handleCreated,
handleChange,
handleDestroyed,
handleFocus,
handleBlur,
customAlert,
customPaste,
abc,
};
}
}3.父組件中
//引入
import WangEditor from '../../../components/WangEditor.vue'
//注冊
components: {
WangEditor
},<a-form-model-item :wrapper-col="{ offset: 2, span: 24 }" name="introduction">
<div>課程介紹:</div><br>
<WangEditor
class="WangEditor"
@select="getRich"
ref="childrenRef"
/>
</a-form-model-item> //當(dāng)編輯器的內(nèi)容更新時,獲取該值
const getRich = function (value){
state.introduction = value
console.log(value)
}
//獲取dom元素
const childrenRef = ref(null)
······
//調(diào)用子組件的方法清空編輯器內(nèi)容
childrenRef.value.abc()總結(jié)
到此這篇關(guān)于Vue3使用富文本框(wangeditor)的文章就介紹到這了,更多相關(guān)Vue3使用富文本框wangeditor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue render函數(shù)實戰(zhàn)之實現(xiàn)tabs選項卡組件
這篇文章主要介紹了Vue render函數(shù)實戰(zhàn)之實現(xiàn)tabs選項卡組件的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
解決vue組件中使用v-for出現(xiàn)告警問題及v for指令介紹
這篇文章主要介紹了解決vue組件中使用v-for出現(xiàn)告警問題,在文中給大家介紹了v for指令,需要的朋友可以參考下2017-11-11
vue3父組件異步props傳值子組件接收不到值問題解決辦法
這篇文章主要給大家介紹了關(guān)于vue3父組件異步props傳值子組件接收不到值問題的解決辦法,需要的朋友可以參考下2024-01-01
vue.config.js配置報錯解決辦法(可能是與webpack混淆)
在Vue.js開發(fā)過程中,vue.config.js文件是用于配置項目的,特別是對于開發(fā)環(huán)境的設(shè)置,這篇文章主要給大家介紹了關(guān)于vue.config.js配置報錯解決的相關(guān)資料,可能是與webpack混淆,需要的朋友可以參考下2024-06-06

