rich-view.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import { useUserStore } from '@/pinia/modules/user'
  18. const userStore = useUserStore()
  19. const emits = defineEmits(['change', 'update:modelValue'])
  20. const editorConfig = ref({
  21. readOnly: true
  22. })
  23. const change = (editor) => {
  24. emits('change', editor)
  25. emits('update:modelValue', valueHtml.value)
  26. }
  27. const props = defineProps({
  28. modelValue: {
  29. type: String,
  30. default: ''
  31. }
  32. })
  33. const editorRef = shallowRef()
  34. const valueHtml = ref('')
  35. // 组件销毁时,也及时销毁编辑器
  36. onBeforeUnmount(() => {
  37. const editor = editorRef.value
  38. if (editor == null) return
  39. editor.destroy()
  40. })
  41. const handleCreated = (editor) => {
  42. editorRef.value = editor
  43. valueHtml.value = props.modelValue
  44. }
  45. watch(() => props.modelValue, () => {
  46. valueHtml.value = props.modelValue
  47. })
  48. </script>
  49. <style scoped lang="scss">
  50. </style>