毕业涉及中使用到了富文本框,所以学习使用了wangeditor富文本框,现进行总结
1.安装
npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue@next --save
2.配置wangeditor组件(src/components/wangeditor.vue)
//script标签中引入
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()
// 内容 HTML
const valueHtml = ref('')
//配置功能栏
let toolbarConfig = {
toolbarKeys: [
'headerSelect',
'blockquote',
'|',
'bold',
'underline',
'italic',
{
key: 'group-more-style',
title: '更多',
iconSvg:
'',
menuKeys: ['through', 'code', 'sup', 'sub']
},
'color',
'bgColor',
'|',
'fontSize',
{
key: 'group-justify',
title: '对齐',
iconSvg:
'',
menuKeys: ['justifyLeft', 'justifyRight', 'justifyCenter', 'justifyJustify']
},
'todo',
'fontFamily',
{
key: 'group-indent',
title: '缩进',
iconSvg:
'',
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: '请输入内容...',
// 所有的菜单配置,都要在 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,()=>{
//当编辑器的内容发生变化时,把值传给父组件
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 数据(如从 word wsp 复制粘贴)
// 自定义插入内容
editor.insertText('xxx')
// 返回 false ,阻止默认粘贴行为
event.preventDefault()
callback(false) // 返回值(注意,vue 事件的返回值,不能用 return)
// 返回 true ,继续默认的粘贴行为
// callback(true)
}
//父组件调用子组件的方法清空编辑器内容
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
},
课程介绍:
//当编辑器的内容更新时,获取该值
const getRich = function (value){
state.introduction = value
console.log(value)
}
//获取dom元素
const childrenRef = ref(null)
······
//调用子组件的方法清空编辑器内容
childrenRef.value.abc()