1.官网api
https://www.wangeditor.com/doc/
2.vue引入
npm i wangeditor --save
创建wang-editor.vue组件
<template>
<div>
<div ref="toolbar" class="toolbar"></div>
<p> ---------------</p>
<div ref="editor" class="text"></div>
</div>
</template>
<script>
import E from 'wangeditor'
export default {
name: 'wangEditor',
data() {
return {
editor: null,
info: null
}
},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: String,
default: ''
},
isClear: {
type: Boolean,
default: false
}
},
watch: {
isClear(val) {
// 触发清除文本域内容
if (val) {
this.editor.txt.clear()
this.info = null
}
},
//value为编辑框输入的内容即父组件的v-model的值
value: function(value) {
if (value !== this.editor.txt.html()) {
this.editor.txt.html(this.value)
}
}
},
mounted() {
this.initEditor()
this.editor.txt.html(this.value)
},
// 销毁编辑器
beforeDestroy() {
this.editor.destroy()
this.editor = null
},
methods: {
initEditor() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor)
this.editor.config.uploadImgShowBase64 = false // 是否设置base64存储图片
this.editor.config.uploadImgServer = '/api/upload/uploadImg'// 配置服务器端地址
this.editor.config.uploadImgHeaders = { }// 自定义 header
this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
// this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
this.editor.config.uploadImgMaxLength = 3 // 限制一次最多上传 3 张图片
// this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
this.editor.config.showLinkImg = false //隐藏插入网络图片的功能
// 设置编辑区域高度为 500px
this.editor.config.height = 500
// 配置菜单
this.editor.config.menus = [
'head', // 标题
'bold', // 粗体
'fontSize', // 字号
'fontName', // 字体
'italic', // 斜体
'underline', // 下划线
'strikeThrough', // 删除线
'foreColor', // 文字颜色
'backColor', // 背景颜色
'link', // 插入链接
'list', // 列表
'justify', // 对齐方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入图片
'table', // 表格
'video', // 插入视频
'code', // 插入代码
'undo', // 撤销
'redo', // 重复
'fullscreen' // 全屏
]
this.editor.config.uploadImgHooks = {
// 上传图片之前
before: function(xhr) {
console.log(xhr)
// 上传之前的一些判断操作,可阻止图片上传
/* return {
prevent: true,
msg: '需要提示给用户的错误信息'
}*/
},
// 图片上传并返回了结果,图片插入已成功
success: function(xhr) {
console.log('success', xhr)
},
// 图片上传并返回了结果,但图片插入时出错了
fail: function(xhr, editor, resData) {
console.log('fail', resData)
},
// 上传图片出错,一般为 http 请求的错误
error: function(xhr, editor, resData) {
console.log('error', xhr, resData)
},
// 上传图片超时
timeout: function(xhr) {
console.log('timeout')
},
// 图片上传并返回了结果,想要自己把图片插入到编辑器中
// 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
customInsert: function(insertImgFn, result) {
// result 即服务端返回的接口
console.log('customInsert', result)
//insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
//我的后端接口返回的格式为{code:0,data:'url地址'}
insertImgFn(result.data)
}
}
this.editor.config.onchange = (html) => {
this.info = html
this.$emit('change', this.info) // 将内容同步到父组件中
}
// 创建富文本编辑器
this.editor.create()
}
}
}
</script>
<style lang="less">
.toolbar {
border: 1px solid #ccc;
}
.text {
border: 1px solid #ccc;
height: 400px;
}
</style>
3.父组件调用
<wangeditor v-model="details" :isClear="isClear" @change="changeEditor"></wangeditor>
import wangeditor from '@/components/wang-editor'
export default {
name: "p-editor",
data() {
return {
details: '', // 双向同步的变量
isClear: false,
id:'1'
};
},
components: {
wangeditor
},
mounted(){
this.getInfoById(this.id)
},
methods:{
//根据id查询数据
getInfoById(id){
axios.post('/api/getById', {id:id}).then(res => {
//回显赋值给富文本编辑器
this.details = res.data.content
})
},
//保存
save(item){
let content = this.details
axios.post('/api/save', {id:this.id,content:content}).then(res => {
if(res.code === 0){
this.$message.success('保存成功');
this.getInfoById(id)
}
})
},
//子组件编辑器的值改变同步赋值
changeEditor(val) {
// console.log(val)
this.details = val
},
}
};
后端接口代码及使用vue-quill编辑器链接如下 :
https://blog.csdn.net/ziyu_nuannuan/article/details/121675984