vue中mavonEditor的使用

文章目录


最近搭博客需要使用文本编辑器,索性使用记录一下。
官网
安装

npm install mavon-editor --save

引入

 // 全局注册
    // import with ES6
    import Vue from 'vue'
    import mavonEditor from 'mavon-editor'
    import 'mavon-editor/dist/css/index.css'
    // use
    Vue.use(mavonEditor)
    new Vue({
        'el': '#main',
        data() {
            return { value: '' }
        }
    })
<div id="main">
    <mavon-editor v-model="value"/>
</div>

配置项
1
工具栏可选属性

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

事件方法
哎哎
开启代码高亮props

    // ishljs默认为true
    <mavon-editor :ishljs = "true"></mavon-editor>

图片上传

<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替换到文本原位置![...](0) -> ![...](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);
           })
        }
    }
}

默认大小样式为 min-height: 300px , min-width: 300px 可自行覆盖
基础z-index: 1500
仅用作展示可以设置props: toolbarsFlag: false , subfield: false, defaultOpen: “preview”

Vue 3.2.13和Mavon Editor是两个不同的库,它们可以在Vue项目中一起使用来实现富文本编辑器的功能。 首先,你需要在你的Vue项目中安装Vue 3.2.13和Mavon Editor。你可以使用npm或yarn来安装它们。在命令行中运行以下命令: ```shell npm install vue@3.2.13 npm install mavon-editor ``` 安装完成后,你需要在你的Vue项目中引入Vue和Mavon Editor。在你的Vue组件中,可以按照以下方式引入它们: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import MavonEditor from 'mavon-editor'; import 'mavon-editor/dist/css/index.css'; const app = createApp(App); app.use(MavonEditor); app.mount('#app'); ``` 在上面的代码中,我们首先引入了Vue和Mavon Editor。然后,我们使用`app.use(MavonEditor)`来注册Mavon Editor插件。最后,我们使用`app.mount('#app')`将Vue应用挂载到HTML页面上的一个元素上。 现在,你可以在你的Vue组件中使用Mavon Editor了。在模板中,你可以使用`<mavon-editor></mavon-editor>`标签来渲染Mavon Editor组件。例如: ```html <template> <div> <mavon-editor v-model="content"></mavon-editor> </div> </template> <script> export default { data() { return { content: '' }; } }; </script> ``` 在上面的代码中,我们使用`v-model`指令将Mavon Editor的内容绑定到了`content`属性上。这样,当用户在编辑器中输入内容时,`content`属性的值也会相应地更新。 这就是使用Vue 3.2.13和Mavon Editor实现富文本编辑器的基本步骤。你可以根据自己的需求进一步定制和配置Mavon Editor的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值