- 官网地址:www.wangeditor.com/
- 支持 vue2 vue3 React
vue2使用demo ↓
一、安装包,可能会出现问题
yarn add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save
yarn add @wangeditor/editor-for-vue
# 或者 npm install @wangeditor/editor-for-vue --save
如果提示:
Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps
在安装指令后面加 --legacy-peer-deps
npm install @wangeditor/editor-for-vue --save --legacy-peer-deps
二、界面模板
<template>
<div style="border: 1px solid #ccc;">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
style="height: 500px; overflow-y: hidden;"
v-model="html"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="onCreated"
/>
</div>
</template>
三、script代码
<script>
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default{
components: { Editor, Toolbar },
data() {
return {
editor: null,
html: '<p>hello</p>',
toolbarConfig: { },
editorConfig: { placeholder: '请输入内容...' },
mode: 'default', // or 'simple'
}
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
},
},
mounted() {
// 模拟 ajax 请求,异步渲染编辑器
setTimeout(() => {
this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
}, 1500)
},
beforeDestroy() {
const editor = this.editor
if (editor == null) return
editor.destroy() // 组件销毁时,及时销毁编辑器
}
}
</script>
四、以上代码是官网的基础示例,如果需要自定义图片上传
比如我公司后端拿到图片后会上传到腾讯云对象存储,再返回给前端图片地址 将变量 editorConfig 的值替换下
editorConfig: {
placeholder: '请输入内容...',
// 所有的菜单配置,都要在 MENU_CONF 属性下
MENU_CONF: {
// 配置上传图片
uploadImage: {
customUpload: this.FileUploadImage
}
}
},
- FileUploadImage是图片上传的函数
- insertFn 是富文本插件上传图片的API,在调接口上传完图片拿到图片地址后调用它
// 富文本自定义传图片
FileUploadImage(file, insertFn) {
const formData = new FormData()
formData.append('file', file)
uploadAction('common/file/upload', formData).then((res) => {
if (res.status === 0) {
insertFn(res.data, file.name, '')
}
})
},
五、封装成组件
<template>
<div class="boxWangEditor">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:default-config="toolbarConfig"
:mode="mode"
/>
<Editor
v-model="content"
:style="{height: `${height}px`}"
:default-config="editorConfig"
:mode="mode"
class="Editor"
@onCreated="onCreated"
@onChange="onChange"
/>
</div>
</template>
<script>
/*
使用示例:
<wangEditor v-model="model.activityContent" />
import wangEditor from '@/components/xm/richText/wangEditor.vue'
components: {
wangEditor
},
官网:https://www.wangeditor.com/
*/
import { uploadAction } from '@/api/manage'
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default {
name: 'RichTextWangEditor',
components: {
Editor, Toolbar
},
model: {
prop: 'value',
event: 'change'
},
props: {
height: {
type: Number,
default: 634
},
value: {
type: String,
default: ''
}
},
data() {
return {
content: '', // 富文本内容
imgCount: 0,
imgMaxCount: 10,
editor: null,
toolbarConfig: { },
editorConfig: {
placeholder: '请输入内容...',
// 所有的菜单配置,都要在 MENU_CONF 属性下
MENU_CONF: {
// 配置上传图片
uploadImage: {
customUpload: this.FileUploadImage
}
}
},
mode: 'default' // or 'simple'
}
},
watch: {
value: {
immediate: true,
handler(newVal, oldVal) {
this.content = newVal
}
}
},
created() {},
beforeDestroy() {
// 销毁富文本组件
const editor = this.editor
if (editor == null) return
editor.destroy() // 组件销毁时,及时销毁编辑器
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
},
// 值更新事件
onChange() {
this.$emit('change', this.content)
},
// 富文本自定义传图片
FileUploadImage(file, insertFn) {
if (this.imgCount > this.imgMaxCount) {
this.$message.warning(`图片最多上传${this.imgMaxCount}张`)
return
}
const formData = new FormData()
formData.append('file', file)
this.imgCount++ // 上传的图片计数
console.log('this.imgCount', this.imgCount)
uploadAction('common/file/upload', formData).then((res) => {
if (res.status === 0) {
insertFn(res.data, file.name, '')
}
})
}
}
}
</script>
<style lang="scss" scoped>
.boxWangEditor{
border: 1px solid #ccc;
z-index: 2000;
.Editor{
overflow-y: hidden;
}
}
</style>