在vue项目中使用MavonEditor,实现上传图片。

啦啦啦啦啦!我又来更新了!

由于ckeditor过于复杂,当然有可能是因为博主的过于愚笨,导致上传图像失败,并且浪费我宝贵的时间,so,我就还是选择一个比较中规中矩的编辑器,够我使用就ok了!

那么重点来咯!

1.首先进进入项目的目录下,摁住shift+右键,,运行

 npm uninstall mavon-editor  命令,它就会自动安装!

2.安装完事之后呢,我们就要在项目中引入了。

打开项目中main.js 引入 mavon-editor

完事,你还需要在main.js中加入

Vue.use(mavonEditor)

Vue.component('iv-row', Row)

3.第三步

在你要用到这个富文本编辑的页面下,使用mavon-editor标签就可以了。

<iv-formItem label="文章内容:">
    <mavon-editor ref=md @imgAdd="imgAdd"  v-model="article.articleContent" :toolbars="toolbars" @change="mavonChangeHandle"/>
</iv-formItem>

4.重点2来咯,菜单栏自定义!!!放到return下。

:toolbars="toolbars"   //就是这个东东
toolbars: {
  bold: true, // 粗体
  italic: true, // 斜体
  header: true, // 标题
  underline: true, // 下划线
  strikethrough: true, // 中划线
  mark: false, // 标记
  superscript: false, // 上角标
  subscript: false, // 下角标
  quote: true, // 引用
  ol: true, // 有序列表
  ul: true, // 无序列表
  link: true, // 链接
  imagelink: true, // 图片链接
  code: true, // code
  table: true, // 表格
  fullscreen: true, // 全屏编辑
  readmodel: false, // 沉浸式阅读
  htmlcode: true, // 展示html源码
  help: false, // 帮助
  /* 1.3.5 */
  undo: true, // 上一步
  redo: false, // 下一步
  trash: false, // 清空
  save: false, // 保存(触发events中的save事件)
  /* 1.4.2 */
  navigation: false, // 导航目录
  /* 2.1.8 */
  alignleft: true, // 左对齐
  aligncenter: true, // 居中
  alignright: true, // 右对齐
  /* 2.2.1 */
  subfield: true, // 单双栏模式
  preview: true, // 预览
  boxShadow: false

5.上传图片也就是

@imgAdd="imgAdd"

这个事件,上代码,不磨磨唧唧,整的我都不严肃了。

这个你得区分来看,这是一个请求,全局路由配置,我就不多说了,我的是这个this.$http.adornUrl,你的是什么,怎么配置的我管不着!

pos你的编辑区内容,下边也说到了会完成替换。

imgAdd (pos, file) {
// 上传图片
  var formData = new FormData()
  formData.append('image', file)
  axios({
    url: this.$http.adornUrl('/article/uploadImageByEditor'),//请求地址
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  }).then((url) => {
    // console.log(JSON.stringify(url))
    // 第二步.将返回的url替换到文本原位置![...](./0) -> ![...](url)
    /**
     * $vm 指为mavonEditor实例,可以通过如下两种方式获取
     * 1.  通过引入对象获取: `import {mavonEditor} from ...` 等方式引入后,`$vm`为`mavonEditor`
     * 2. 通过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>,`$vm`为 `this.$refs.md`
    * 3. 由于vue运行访问的路径只能在static下,so,我就把图片保存到它这里了
     */
    this.$refs.md.$img2Url(pos, 'http://localhost:8002/static/image/' + url.data.data)
  })

为了大家写代码能力提升,后台代码我就不贴了,百度一大堆哦!!!!!

完事,拜拜!

加入我们群

如果有需要,欢迎可以加入我们的QQ群!(QQ搜索 816175200,加入我们的QQ群吧!)
有任何问题,也可以加入我们的QQ群,欢迎交(che)流(dan)!也欢迎参观我的博客www.aquestian.cn

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
Vue 3使用mavon-editor,首先需要在main.js文件进行引入和注册。你可以按照以下步骤进行操作: 1. 在main.js文件使用ES6的import语法引入Vue和mavon-editor: ```javascript import Vue from 'vue'; import mavonEditor from 'mavon-editor'; import 'mavon-editor/dist/css/index.css'; ``` 2. 使用Vue.use()方法全局注册mavon-editor: ```javascript Vue.use(mavonEditor); ``` 3. 确保在main.js文件引入了mavon-editor的样式文件'mavon-editor/dist/css/index.css'。 这样,你就可以在Vue 3使用mavon-editor了。你可以在组件使用mavon-editor的标签,例如: ```html <template> <div> <mavon-editor v-model="value"></mavon-editor> </div> </template> <script> export default { data() { return { value: '' } } } </script> ``` 这样就可以在Vue 3使用mavon-editor进行富文本编辑了。 #### 引用[.reference_title] - *1* [VueVue-cli2结合mavonEditor实现MarkDown编辑器](https://blog.csdn.net/KaiSarH/article/details/115255879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [在 Vue 3 安装和使用 mavon-editor富文本编辑器](https://blog.csdn.net/qq_51447496/article/details/131412288)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

答 案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值