天天看點

Vue--整合mavon-editor編輯器(markdown編輯和預覽)

簡介

說明

        本文介紹Vue如何使用markdown編輯器。

        mavon-editor是目前比較主流的markdown編輯器,本文介紹它的使用方法。

官網網址

​​https://github.com/hinesboy/mavonEditor​​

安裝mavon-editor依賴

npm install mavon-editor -P      

注冊mavon-editor編輯器

import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor);      

使用編輯功能

代碼

<template>
  <div class="app-container">
    <el-button type="primary" @click="saveGuideData()">釋出</el-button>

    <el-form :model="guideDetail" :rules="rules" ref="dataForm" label-width="100px">
      <el-form-item label="内容" prop="content">
        <mavon-editor v-model="guideDetail.content"></mavon-editor>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {saveGuide} from "@/api/guide";
export default {
  name: "GuideEdit",
  data() {
    return {
      guideDetail: {
        content: ''
      },
      rules: {
        content: [
          {required: true, message: '請輸入内容', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    saveGuideData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveGuide(this.guideDetail.content).then(() => {
            this.$notify({
              title: '成功',
              message: '建立成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>      

結果展示

使用預覽功能

代碼

<template>
  <div class="app-container">
    <mavon-editor v-model="guideDetail.content"
                  :subfield="false"
                  :defaultOpen="'preview'"
                  :editable="false"
                  :toolbarsFlag="false"
                  >
    </mavon-editor>
  </div>
</template>

<script>
export default {
  name: "GuideDetail",
  data() {
    return {
      guideDetail: {
        content: '### 這是第三級标題\n' +
            '這裡是正文'
      },
    }
  }
}
</script>

<style scoped>

</style>      

結果展示