最近作一个项目,须要在vue项目中接入 markdown 编辑器,其实这个好接,他没有什么特别的样式,男的就是图片的上传。
今天给你们推荐一个插件 :mavonEditor
这个是他的github:https://github.com/hinesboy/mavonEditor/blob/master/README.md
javascript
这个插件支持自定义界面,代码高亮,图片上传等,是我见过最好的一个。
他的使用方法在github上写的很详细,能够去里面看。css
$ npm install mavon-editor --save 或者 $ yarn add mavon-editor
import mavonEditor from 'mavon-editor' import 'mavon-editor/dist/css/index.css' Vue.use(mavonEditor)
在须要引入markdown编辑器的界面html
<template> <div> <mavon-editor v-model="value"/> </div> </template> <script> export default { data() { return { value: '', defaultData: "preview" }; }, }; </script>
<article v-html="value" ></article> <script> export default { data() { return {//value的值是通过markdown解析后的文本,可以使用`@change="changeData"`在控制台打印显示 value: `<blockquote> <p>你好</p> </blockquote> <p><code>java</code></p>`, defaultData: "preview" }; }, methods: { } }; </script>
<template> <mavon-editor ref=md @imgAdd="$imgAdd" @imgDel="$imgDel"></mavon-editor> </template> exports default { methods: { // 绑定@imgAdd event $imgAdd(pos, $file){ // 第一步.将图片上传到服务器. var formdata = new FormData(); formdata.append('image', $file); axios({ url: 'server url', method: 'post', data: formdata, headers: { 'Content-Type': 'multipart/form-data' }, }).then((url) => { // 第二步.将返回的url替换到文本原位置 ->  /** * $vm 指为mavonEditor实例,能够经过以下两种方式获取 * 1. 经过引入对象获取: `import {mavonEditor} from ...` 等方式引入后,`$vm`为`mavonEditor` * 2. 经过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>,`$vm`为 `this.$refs.md` */ $vm.$img2Url(pos, url); }) } } }
完成!vue