在Vue中使用mavonEditor

安装

npm install mavon-editor --save

引入

//在 main.js 全局注册
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor);

基础使用

<mavon-editor
	v-model="value"
	:ishljs="true"
	@imgAdd="$imgAdd"
	@imgDel="$imgDel"
	@save="saveDoc">
</mavon-editor>

methods:

saveDoc(value, render) {
	console.log(value); //value:md格式
	console.log(render);//render:html格式
}

显示:

<article v-html="html" style="height: 270px"></article>

如果只想存储md格式的值,可以使用编辑器自带的markdown-it进行解析。
首先还是在main.js注册

Vue.prototype.$markDown = mavonEditor.markdownIt;

使用:

this.html = this.$markDown.render(你保存的md格式数据)

markdown-it 中文文档
mavonEditor这样介绍Markdown-It的

图片上传

import axios from 'axios';
$imgAdd(pos, $file){
  let formData = new FormData();
  formData.append('image', $file);
  let self = this;
  axios({
    url: 'you url', 
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' },
  }).then(function (response) { 
    if (200 != response.data.code)
    {
      self.$Message.error({
              content: '上传图片失败!格式不正确或图片超过2M ',
              duration: 5,
              closable: true,
              background:true
            });
    }else{
      self.$refs.md.$img2Url(pos, response.data.imagePath); //后端处理返回一个imagePath
    }
  })
},
$imgDel (pos) {
  //delete pos;
  console.log(pos)
},

同时上传多个图片,参考官方
上传后返回imagePath将替换文本原位置,如图
在这里插入图片描述
github上mavonEditor文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值