1、安装npm install wangeditor
2、封装组件
<template lang="html"> <div class="editor"> <div ref="toolbar" class="toolbar"></div> <div ref="editor" class="text"></div> </div> </template>
<script> import E from 'wangeditor' export default { name: 'editoritem', data() { return { editor: null, info_: null } }, model: { prop: 'value', event: 'change' }, props: { value: { type: String, default: '' }, disable: { type: null, default: null }, isClear: { type: Boolean, default: false } }, 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) } } }, mounted() { this.seteditor() this.editor.txt.html(this.value) }, methods: { seteditor() { this.editor = new E(this.$refs.toolbar, this.$refs.editor) this.editor.customConfig = this.editor.customConfig ? this.editor.customConfig : this.editor.config this.editor.customConfig.uploadImgShowBase64 = false // base 64 存储图片 this.editor.customConfig.uploadImgHeaders = {} // 自定义 header this.editor.customConfig.uploadFileName = 'file' // 后端接受上传文件的参数名 this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M this.editor.customConfig.uploadImgMaxLength = 10 // 限制一次最多上传 10 张图片 this.editor.customConfig.uploadImgTimeout = 5 * 60 * 1000 // 设置超时时间 this.editor.customConfig.customUploadImg = async (files, insertImgFn) => { //自定义上传功能,注释部分是举例说明 // for (let file of files) { // let formdata = new FormData() // formdata.append('files', file) // var obj = {} // obj.createPersonId = this.$userInfo.EmployeeID // formdata.append( // 'fileRecordDto', // new Blob([JSON.stringify(obj)], { type: 'application/json' }) // ) // this.$uplod.post('file/upload', formdata).then(res => { // for (let index = 0; index < res.data.length; index++) { // const element = res.data[index] // insertImgFn(element.fileUrl) //上传成功后把图片插入编辑器里 // } // }) // } } // 配置菜单 this.editor.customConfig.menus = [ 'undo', // 撤销 'redo', // 重复 'head', // 标题 'bold', // 粗体 'fontSize', // 字号 'fontName', // 字体 'italic', // 斜体 'underline', // 下划线 'strikeThrough', // 删除线 'foreColor', // 文字颜色 'backColor', // 背景颜色 'link', // 插入链接 'list', // 列表 'justify', // 对齐方式 'quote', // 引用 'emoticon', // 表情 'image', // 插入图片 'table', // 表格 'code' // 插入代码 ] this.editor.customConfig.onchange = html => { this.info_ = html if (!this.disable) { this.$emit('change', this.info_) // 将内容同步到父组件中 } } this.editor.create() // 创建富文本编辑器 if (this.disable) { this.editor.disable() //只读 } else { this.editor.enable() } } } } </script>
<style lang="css"> .editor { width: 100%; margin: 0 auto; position: relative; z-index: 0; } .toolbar { border: 1px solid #ccc; display: flex; flex-wrap: wrap; } .text { border: 1px solid #ccc; border-top: none; min-height: 500px; } </style>
3、使用方法
<wang-editor v-model="this.Content" :disable="1" @change="change"></wang-editor> //使用 import WangEditor from '../../components/WangEditor' //引入组件 export default { components: { WangEditor//挂载 }, data() { return {} } }
效果展示