Vue富文本编辑器使用教程
1.安装
yarn add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save
yarn add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save
2.创建组件
在components下创建富文本编辑器组件

组件内部详情代码(已整理好)
<template>
<div style="border: 1px solid #ccc">
<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode"/>
<Editor style="height: 150px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig" :mode="mode"
@onCreated="handleCreated" @onChange="updateHtml"/>
</div>
</template>
<script>
import '@wangeditor/editor/dist/css/style.css'
import {onBeforeUnmount, ref, shallowRef, onMounted, watch} from 'vue'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'
export default {
components: {Editor, Toolbar},
props: ['html'],
setup(props, {emit}) {
const editorRef = shallowRef()
const valueHtml = ref(props.html)
watch(props, (newValue, oldValue) => {
valueHtml.value = newValue.html
})
const updateHtml = (val) => {
emit('change', valueHtml.value)
}
const toolbarConfig = {
toolbarKeys: [
'bold',
'italic',
'through',
'underline',
'bulletedList',
'numberedList',
'color',
'insertLink',
'fontSize',
'lineHeight',
'uploadImage',
'delIndent',
'indent',
'deleteImage',
'divider',
'insertImage',
'insertTable',
'justifyCenter',
'justifyJustify',
'justifyLeft',
'justifyRight',
'undo',
'redo',
'clearStyle',
'fullScreen'
],
excludeKeys: [
'bgColor',
'blockquote',
'codeBlock',
'emotion',
'fontFamily',
'headerSelect'
]
}
const editorConfig = {
placeholder: '请输入内容...',
}
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
const handleCreated = (editor) => {
editorRef.value = editor
}
return {
editorRef,
valueHtml,
mode: 'default',
toolbarConfig,
editorConfig,
handleCreated, updateHtml
}
}
}
</script>
以上已经配置好了父子组件传值,之后就可以直接调用来进行使用了
调用部分
<MyEditor @change="(html) => { add.formModel.description = html }" :html="add.formModel.description"/>