vue使用富文本编辑器上传图片:
我是用的是wangEditor 富文本编辑器 demo:http://www.wangeditor.com/
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'let editor=nullexport default {name: 'Editorbar',data () {return {info_: null}},model: {prop: 'value',event: 'change'},props: {value: {type: String,default: ''},isClear: {type: Boolean,default: false}},watch: {isClear (val) {// 触发清除文本域内容if (val) {editor.txt.clear()this.info_ = null}},value (val) {// 使用 v-model 时,设置初始值 editor.txt.html(val)}},mounted () {this.seteditor()},methods: {seteditor () {editor = new E(this.$refs.toolbar, this.$refs.editor)editor.customConfig.uploadImgShowBase64 = true // base 64 存储图片 editor.customConfig.uploadImgServer = 'http://47.106.198.122/api/CanteenAdmin/CanteenManagement/UploadImage'// 配置服务器端地址 editor.customConfig.uploadImgHeaders = { 'token':sessionStorage.getItem('token') }// 自定义 header editor.customConfig.uploadFileName = 'image' // 后端接受上传文件的参数名 editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上传 3 张图片 editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间// this.editor.customConfig.uploadParams = {formFile:'2'};// 配置菜单 editor.customConfig.menus = ['bold', // 粗体'underline', // 下划线'link', // 插入链接'quote', // 引用'emoticon', // 表情'image', // 插入图片'table', // 表格'code', // 插入代码 ]editor.customConfig.uploadImgHooks = {fail: (xhr, editor, result) => {// 插入图片失败回调 },success: (xhr, editor, result) => {// 图片上传成功回调// // let imgUrl = result.data;// insertImg(imgUrl) },timeout: (xhr, editor) => {// 网络超时的回调 },error: (xhr, editor) => {console.log(editor)// 图片上传错误的回调 },customInsert: (insertImg, result, editor) => {// 图片上传成功,插入图片的回调 console.log(result);// if(result.code == 200){var url = result.data;insertImg(url)//将内容插入到富文本中// console.log(insertImg(url)+"DFDF")// console.log(data+"dsfd")// } }};editor.customConfig.onchange = (html) => {this.info_ = html // 绑定当前逐渐地值this.$emit('change', this.info_) // 将内容同步到父组件中 console.log(this.info_ )};// 创建富文本编辑器 editor.create()}}} </script><style lang="css">.editor {width: 80%;margin-left: 4%;}.toolbar {border: 1px solid #ccc;}.w-e-menu{z-index:1 !important;}.text {width:100%;border: 1px solid #ccc;height: 300px;} </style>
<!--富文本编辑器--><div style="margin-top:2%;margin-left: 1%"><editor-bar v-model="zldata.info" :isClear="isClear"></editor-bar></div>js: <script> import EditorBar from './editor'export default {data: function () {return {isClear: false,zldata: {info:'',},}},components: {EditorBar},}</script>
喜欢的给个赞吧!!!