这里我用了tinymce,主要解决的是上传图片回调处理
效果预览:
第一步:下载tinymce资源,
把tinymce放到static目录
在index.html中引入tinymce.min.js文件
<script src=<%= htmlWebpackPlugin.options.path %>/tinymce/tinymce.min.js></script>
第二步:新建tinymce组件
template:
<template> <div class="comp-tinymce-wrapper"> <textarea :id="tinymceId"></textarea> </div> </template>
script:
<script>
//上传图片方法
import {uploadRichImg} from '@/api/common' export default { name:'tinymce', props:{ id:{ type:String }, value:{ type:String, default:'' },
//可以自定义toolbar toolbar:{ type:Array, require:false, default(){ return ['removeformat undo redo | bullist numlist | outdent indent | fullscreen code', 'formatselect | bold italic strikethrough forecolor blockquote | alignleft aligncenter alignright | image link '] } }, menubar:{ default:'' }, height:{ type:Number, requier:false, default:400 } }, data(){ return { tinymceId: this.id || 'vue-tinymce'+ Date.parse(new Date()), hasInit:false, hasChange:false } }, mounted(){ this.initTinymce() }, watch:{ value(newV,oldV){ //当传入值不变化的时候更新富文本内容, if(this.hasInit&&!this.hasChange){ window.tinymce.get(this.tinymceId).setContent(newV) } } }, methods:{ initTinymce(){ const _this = this tinymce.init({ selector: `#${this.tinymceId}`, height: this.height, body_class: 'panel-body rich-text', theme: 'modern', content_style:'{p{color_red} }', //是否可拉伸 resize:false, //语言 language: 'zh_CN', //是否显示POWERED BY TINYMCE branding:false, //toolbar toolbar: this.toolbar, fixed_toolbar_container: '#mytoolbar', //menubar menubar: this.menubar, //插件 plugins: 'advlist,autolink,code,paste,textcolor, colorpicker,fullscreen,link,lists, image ', powerpaste_word_import: 'clean', //tinymce内容自定义样式 content_css:'static/tinymce/css/rewrite-mce.css', //源代码弹出层宽 code_dialog_height: 450, //源代码弹出层高 code_dialog_width: 1000, // block_formats: 'Paragraph=p;Header 1=h1;Header 2=h2;Header 3=h3', imagetools_cors_hosts: ['wpimg.wallstcn.com', 'wallstreetcn.com'], //链接打开方式 default_link_target: '_blank', images_upload_url: '/oss/file/uploadRichImg?dir=image',
//上传图片回调 images_upload_handler:(blobInfo, success, failure) => { let fd = new FormData() fd.append('file',blobInfo.blob()) uploadRichImg(fd).then(res => { let result = res.data[0] success(result.url) }) .catch(err => { }) }, init_instance_callback: editor => { if (_this.value) { editor.setContent(_this.value) } _this.hasInit = true editor.on('NodeChange Change input KeyUp', () => { //change触发watch去setContent,光标变化了, this.hasChange = true this.$emit('input', editor.getContent({ format: 'raw' })) }) } }) }, destroyTiny(){ if(window.tinymce.get(this.tinymceId)){ window.tinymce.get(this.tinymceId).destroy() } }, setContent(val){ window.tinymce.get(this.tinymceId).setContent(val) }, getContent(){ window.tinymce.get(this.tinymceId).getContent() }, destroyed(){ this.destroyTiny() } } } </script>
在父组件中引入tinymce组件
import tinymce from '@/components/Tinymce/index'
<tinymce :height="tiny.height" v-model="sendObj.content"></tinymce>
script:
export default { name: 'addNews', components:{ tinymce }, data(){ return { tiny:{ height:300 }, sendObj:{ content:'nihao' } }, methods:{ } } </script>
Live and learn ;)