话不多说,先看长啥样
看完了效果,首先是wangEditor3的基本封装,html代码如下:
<template>
<div class="editor">
<div ref="editor" class="text"></div>
<ImgBox v-show="false" ref="ImgBox" type="image" @imgList="imgList"></ImgBox>
//这个是自定义的组件
</div>
</template>
js代码如下:
定义需要的参数
import E from 'wangeditor'
//引入wangeditor
//贴上版本 "wangeditor": "^4.5.2"
import ImgBox from './ImgBox'
data() {
return {
uploadPath: '',
editor: null,
info_: null
}
},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: String,
default: ''
},
isClear: {
type: Boolean,
default: false
},
menus: {
type: Array,
default: () => {
return [
'head', // 标题
'bold', // 粗体
'fontSize', // 字号
'fontName', // 字体
'italic', // 斜体
'underline', // 下划线
'strikeThrough', // 删除线
'foreColor', // 文字颜色
'backColor', // 背景颜色
'link', // 插入链接
'list', // 列表
'justify', // 对齐方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入图片
'table', // 表格
'video', // 插入视频
'code', // 插入代码
'undo', // 撤销
'redo', // 重复
'fullscreen', // 全屏
'insertABC'
]
}
},
fullScreen: {
type: Boolean,
default: false
},
height: {
type: Number,
default: 300
}
参数不了解的链接在这------------wangeditor官网
接着是逻辑:
watch: {
isClear(val) {
// 触发清除文本域内容
if (val) {
this.editor.txt.clear()
this.info_ = null
}
},
value: function(value) {
if (value !== this.editor.txt.html()) {
this.editor.txt.html(this.value)
}
}
//value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
},
mounted() {
this.seteditor()
this.editor.txt.html(this.value)
},
methods: {
seteditor() {
const self = this;
// http://192.168.2.125:8080/admin/storage/create
this.editor = new E(this.$refs.editor)
this.editor.config.uploadImgShowBase64 = false // base 64 存储图片
this.editor.config.uploadImgServer = '' // 配置服务器端地址
this.editor.config.uploadImgHeaders = {} // 自定义 header
this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
this.editor.config.uploadImgMaxLength = 6 // 限制一次最多上传 3 张图片
this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
this.editor.config.pasteIgnoreImg = false
this.editor.config.showLinkImg = false
// 配置菜单
this.editor.config.menus = this.menus
// 设置编辑区域的高度
this.editor.config.height = this.height - 24
// 设置显示全屏按钮
this.editor.config.showFullScreen = this.fullScreen
const { BtnMenu } = E
const editors = this.editor
// 创建 class
class InsertABCMenu extends BtnMenu {
constructor(editors) {
const $elem = E.$(
`
<div class="w-e-menu">
<span class="el-icon-upload2"></span>
</div>
`
);
super($elem, editors);
}
// 自定义菜单点击事件
clickHandler() {
self.open()
}
// 菜单激活状态
tryChangeActive() {
console.log('菜单激活')
// this.active(); // 菜单激活
// this.unActive() // 菜单不激活
}
}
// 注册菜单
this.editor.menus.extend("insertABC", InsertABCMenu);
// 重新配置 editor.config.menus
this.editor.config.menus = this.editor.config.menus.concat("insertABC");
this.editor.config.uploadImgHooks = {
fail: (xhr, editor, result) => {
console.log(xhr, editor, result)
// 插入图片失败回调
},
success: (xhr, editor, result) => {
console.log(xhr, editor, result)
// 图片上传成功回调
},
timeout: (xhr, editor) => {
console.log(xhr, editor)
// 网络超时的回调
},
error: (xhr, editor) => {
console.log(xhr, editor)
// 图片上传错误的回调
},
customInsert: (insertImg, result, editor) => {
console.log(insertImg, editor, result)
// 图片上传成功,插入图片的回调
//result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...]
// console.log(result.data[0].url)
//insertImg()为插入图片的函数
//循环插入图片
// for (let i = 0; i < 1; i++) {
// console.log(result)
let url = this.uploadPath + result.url
insertImg(url)
// }
}
}
this.editor.config.onchange = html => {
this.info_ = html // 绑定当前逐渐地值
this.$emit('change', this.info_) // 将内容同步到父组件中
}
// 创建富文本编辑器
this.editor.create()
},
open(){
//组件自定义的事件
console.log('open')
this.$refs.ImgBox.toSeleteMaterial()
},
imgList(data){
//选择图片后的回调
console.log('------',data)
data.forEach(item=>{
this.editor.txt.append(`<img src=${item}> `)
})
}
}
贴上父组件使用方法代码如下:
<RzTextEditor
v-model="formValidate.details"
:isClear="isClear"
:menus="editorMenus"
:height="650"
@change="change"/>
粘贴完代码别忘了给作者点个赞~~~~~~~~~