1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 前端使用富文本编辑器wangEditor

前端使用富文本编辑器wangEditor

时间:2021-07-14 16:52:52

相关推荐

前端使用富文本编辑器wangEditor

一、npm下载

终端输入

npm i wangeditor --save

二、代码

此处已经把富文本编辑器抽出为组件:

组件内容:

<template lang="html"><div><div id="edit"></div></div></template><script>//引入import wangEditor from 'wangeditor'export default {name: 'editoritem',data() {return {//这个为富文本编辑器editor: null,//这个为富文本的内容editorData: ''}},mounted() {//判断哪个id或者class的div为富文本编辑器const editor = new wangEditor(`#edit`)//设置自定义上传的图片为base64,根据需要更改editor.config.uploadImgShowBase64 = true//菜单,包括顺序editor.config.menus = ['head','bold','fontSize','fontName','italic','underline','strikeThrough','indent','lineHeight','foreColor','backColor','link','list',// 'todo','justify','quote','emoticon','image','video','table',// 'code','splitLine','undo','redo',]// 配置 onchange 回调函数,将数据同步到 vue 中editor.config.onchange = (newHtml) => {this.editorData = newHtmlconsole.log(this.editorData)}// 创建编辑器editor.create()this.editor = editor},beforeDestroy() {//vue需要的 调用销毁 API 对当前编辑器实例进行销毁this.editor.destroy()this.editor = null}}</script><style scoped></style>

父组件引用:

//引用注册<template><div><wang-enduit/></div></template><script>import wangEnduit from '../../wangEnduit/index'export default {name: "dataAdd",components:{wangEnduit}}</script>

图片上传有base64和文字一起上传,也有传入url地址,看你的情况自己改。

上传图片 · wangEditor 用户文档

富文本内容为data里面的editorData,可以用自定义事件传输到父组件,也可以用vuex,根据你的情况看着来。

相对于其他富文本编辑器,这个可以直接安装复制使用,也不用去官网注册买服务什么的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。