与其庸人自扰,不如看淡点好。
在安装好脚手架的依赖后,要执行 npm install vue-html5-editor -S 来安装这个富文本插件
由于这个富文本插件的图标是依赖font-awesome.css的,所以要npm install font-awesome --save
安装这个css然后在main.js中引入这个css import 'font-awesome/css/font-awesome.min.css'
然后按照文档将其注册
import VueHtml5Editor from 'vue-html5-editor'
// import initRichText from './common/initHTMLEditor.js'
import 'font-awesome/css/font-awesome.min.css'
let opt = {
// 全局组件名称,使用new VueHtml5Editor(options)时该选项无效
name: "vue-html5-editor",
// 是否显示模块名称,开启的话会在工具栏的图标后台直接显示名称
showModuleName: true,
// 自定义各个图标的class,默认使用的是font-awesome提供的图标
icons: {
text: "fa fa-pencil",
color: "fa fa-paint-brush",
font: "fa fa-font",
align: "fa fa-align-justify",
list: "fa fa-list",
link: "fa fa-chain",
unlink: "fa fa-chain-broken",
tabulation: "fa fa-table",
image: "fa fa-file-image-o",
hr: "fa fa-minus",
eraser: "fa fa-eraser",
undo: "fa-undo fa",
"full-screen": "fa fa-arrows-alt",
info: "fa fa-info",
},
// 配置图片模块
image: {
// 文件最大体积,单位字节
sizeLimit: 512 * 1024 * 10,
// 上传参数,默认把图片转为base64而不上传
// upload config,default null and convert image to base64
upload: {
// url: 'https://test.xiujianshen.com/media/upload',
url: '/media/upload',
headers: {
'Authorization': '6e1e2d1b88f446c7bea9785febec1c6b-fa06ec7a7065ce25408d85f54d67d6acc93ec390'
},
params: {
prefix: 'coursesuggestImg', // 阿里云额外参数
},
fieldName: 'file'
},
// 压缩参数,默认使用localResizeIMG进行压缩,设置为null禁止压缩
// width和height是文件的最大宽高
compress: {
width: 300,
height: null,
quality: 80
},
// 响应数据处理,最终返回图片链接
uploadHandler(responseText){
console.log(JSON.parse(responseText));
// default accept json data like {ok:false,msg:"unexpected"} or {ok:true,data:"image url"}
var json = JSON.parse(responseText);
if(json.code == 0){
return json.data;
}else{
alert(json.message)
}
}
},
// 语言,内建的有英文(en-us)和中文(zh-cn)
language: "zh-cn",
// 自定义语言
i18n: {
"zh-cn": {
"align": "对齐方式",
"image": "图片",
"list": "列表",
"link": "链接",
"unlink": "去除链接",
"table": "表格",
"font": "文字",
"full screen": "全屏",
"text": "排版",
"eraser": "格式清除",
"info": "关于",
"color": "颜色",
"please enter a url": "请输入地址",
"create link": "创建链接",
"bold": "加粗",
"italic": "倾斜",
"underline": "下划线",
"strike through": "删除线",
"subscript": "上标",
"superscript": "下标",
"heading": "标题",
"font name": "字体",
"font size": "文字大小",
"left justify": "左对齐",
"center justify": "居中",
"right justify": "右对齐",
"ordered list": "有序列表",
"unordered list": "无序列表",
"fore color": "前景色",
"background color": "背景色",
"row count": "行数",
"column count": "列数",
"save": "确定",
"upload": "上传",
"progress": "进度",
"unknown": "未知",
"please wait": "请稍等",
"error": "错误",
"abort": "中断",
"reset": "重置"
}
},
// 隐藏不想要显示出来的模块
hiddenModules: [],
// 自定义要显示的模块,并控制顺序
visibleModules: [
// "text",
// "color",
"font",
"align",
// "list",
// "link",
// "unlink",
// "tabulation",
"image",
// "hr",
// "eraser",
"undo",
// "full-screen",
// "info",
],
// 扩展模块,具体可以参考examples或查看源码
// extended modules
modules: {
//omit,reference to source code of build-in modules
}
};
Vue.use(VueHtml5Editor, opt);
然后这样使用
<template>
<div class="coursesuggest">
<div class="addBtn" @click="addBtn">保存</div>
<vue-html5-editor :content="text" :height="400" placeholder="选填,可上传图片" @change="updateData"></vue-html5-editor>
</div>
</template>
我将其样式修改了 所以我的代码我是这个样子的 将我的所有代码进行附上
<template>
<div class="coursesuggest">
<div class="addBtn" @click="addBtn">[外链图片转存失败(img-L1zB4yuW-1562156712780)(https://mp.csdn.net/assets/img/hold@2x.png)] 保存</div>
[外链图片转存失败(img-XKLbQf6l-1562156712791)(https://mp.csdn.net/assets/img/pictureBox@2x.png)]
<vue-html5-editor :content="text" :height="400" placeholder="选填,可上传图片" @change="updateData"></vue-html5-editor>
</div>
</template>
<script>
import { Toast } from 'mint-ui';
export default {
data() {
return {
text: '',
courseId: undefined,
}
},
methods: {
updateData(e) {
this.text = e;
},
},
}
</script>
<style lang="less" scoped>
.coursesuggest {
height: 100%;
/deep/ .vue-html5-editor {
height: 100%;
}
/deep/ .toolbar {
position: fixed;
left: 0;
top: 0;
width: 100%;
background-color: rgb(241, 241, 243);
ul{
width: 50%;
display: flex;
align-items: center;
justify-content: center;
&>li:nth-child(1) {
display: none;
}
&>li:nth-child(2) {
display: none;
}
&>li:nth-child(4) {
display: none;
}
}
}
/deep/ .vue-html5-editor>.content {
padding-top: 0.8rem;
}
/deep/ .fa-file-image-o:before {
display: none;
}
.addBtn {
position: fixed;
right: 0;
top: 0;
width: 50%;
z-index: 10000;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
height: 37px;
line-height: 37px;
border-bottom: 1px solid #ddd;
img {
height: 16px;
width: 16px;
vertical-align: middle;
margin-right: 5px;
}
}
.pictureBox {
position: fixed;
left: 1.4rem;
top: .18rem;
z-index: 10000;
height: 16px;
width: 16px;
vertical-align: middle;
}
}
</style>
相信你看完会有所收获
在最后附上我的QQ: 2489757828 有问题的话可以找我一同探讨
我的私人博客: 李大玄