当前位置: 首页 > 工具软件 > tui.editor > 使用案例 >

tui-editor预览组件

刘嘉木
2023-12-01

1、组件markdownViewer.vue

<template>
  <div>
    <div :id="id" />
  </div>
</template>

<script>
// deps for editor
import 'codemirror/lib/codemirror.css' // codemirror
import 'tui-editor/dist/tui-editor.css' // editor ui
import 'tui-editor/dist/tui-editor-contents.css' // editor content

import Viewer from 'tui-editor/dist/tui-editor-Viewer'
// import defaultOptions from './default-options'
export default {
  name: 'MarddownViewer',
  props: {
    id: {
      type: String,
      required: false,
      default() {
        return 'viewer'
      }
    },
    value: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      required: false,
      default: '300px'
    }
  },
  data() {
    return {
      editor: null
    }
  },
  watch: {
    value(newValue, preValue) {
      this.setValue(newValue)
    },
    language(val) {
      this.destroyViewer()
      this.initViewer()
    },
    height(newValue) {
      this.editor.height(newValue)
    }
  },
  mounted() {
    this.initViewer()
  },
  destroyed() {
    this.destroyViewer()
  },
  methods: {
    initViewer() {
      this.editor = new Viewer({
        el: document.getElementById(this.id),
        viewer: true,
        height: this.height,
        initialValue: this.value
      })
    },
    destroyViewer() {
      if (!this.editor) return
      this.editor.remove()
    },
    setValue(newValue) {
      this.editor.setValue(newValue)
    }
  }
}
</script>

2、页面引入组件

<markdown-viewer id="fileViewer" ref="fileViewer" :value="content" height="520px" />
 类似资料: