Vue3使用富文本框(wangeditor)的方法總結(jié)
畢業(yè)涉及中使用到了富文本框,所以學(xué)習(xí)使用了wangeditor富文本框,現(xià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: '縮進', 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)文章
vue3+vite 動態(tài)引用靜態(tài)資源及動態(tài)引入assets文件夾圖片的多種方式
通過require動態(tài)引入, 發(fā)現(xiàn)報錯:require is not defind,這是因為 require 是屬于 Webpack 的方法,本文給大家介紹vue3+vite 動態(tài)引用靜態(tài)資源及動態(tài)引入assets文件夾圖片的多種方式,感興趣的朋友一起看看吧2023-10-10解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題
這篇文章主要介紹了解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue動態(tài)菜單、動態(tài)路由加載以及刷新踩坑實戰(zhàn)
這篇文章主要給大家介紹了關(guān)于vue動態(tài)菜單、動態(tài)路由加載以及刷新踩坑的相關(guān)資料,踩的這些坑其實是挺常見的,大家可以看看,避免遇到的時候再踩到同樣的坑,需要的朋友可以參考下2021-10-10Vue3實現(xiàn)LuckSheet在線預(yù)覽Excel表格
在前端開發(fā)中預(yù)覽Excel文件是常見的需求之一,本文將介紹如何使用Vue.js框架以及兩個優(yōu)秀的Excel庫——LuckyExcel和Luckysheet,來實現(xiàn)Excel文件在線預(yù)覽功能,希望對大家有所幫助2023-11-11vue實現(xiàn)動態(tài)給data函數(shù)中的屬性賦值
這篇文章主要介紹了vue實現(xiàn)動態(tài)給data函數(shù)中的屬性賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09