rich-view.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <template>
  2. <div class="border border-solid border-gray-100 h-full">
  3. <Editor
  4. v-model="valueHtml"
  5. class="overflow-y-hidden mt-0.5"
  6. :default-config="editorConfig"
  7. mode="default"
  8. @onCreated="handleCreated"
  9. @onChange="change"
  10. />
  11. </div>
  12. </template>
  13. <script setup>
  14. import '@wangeditor/editor/dist/css/style.css' // 引入 css
  15. import { onBeforeUnmount, ref, shallowRef, watch } from 'vue'
  16. import { Editor } from '@wangeditor/editor-for-vue'
  17. const emits = defineEmits(['change', 'update:modelValue'])
  18. const editorConfig = ref({
  19. readOnly: true
  20. })
  21. const change = (editor) => {
  22. emits('change', editor)
  23. emits('update:modelValue', valueHtml.value)
  24. }
  25. const props = defineProps({
  26. modelValue: {
  27. type: String,
  28. default: ''
  29. }
  30. })
  31. const editorRef = shallowRef()
  32. const valueHtml = ref('')
  33. // 组件销毁时,也及时销毁编辑器
  34. onBeforeUnmount(() => {
  35. const editor = editorRef.value
  36. if (editor == null) return
  37. editor.destroy()
  38. })
  39. const handleCreated = (editor) => {
  40. editorRef.value = editor
  41. valueHtml.value = props.modelValue
  42. }
  43. watch(
  44. () => props.modelValue,
  45. () => {
  46. valueHtml.value = props.modelValue
  47. }
  48. )
  49. </script>
  50. <style scoped lang="scss"></style>