index.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <script setup lang="ts">
  2. import "@wangeditor/editor/dist/css/style.css"; // 引入 css
  3. import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  4. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  5. import { onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
  6. import Edit from "@iconify-icons/ep/edit";
  7. defineOptions({
  8. name: "Editor"
  9. });
  10. const mode = "default";
  11. // 编辑器实例,必须用 shallowRef
  12. const editorRef = shallowRef();
  13. // 内容 HTML
  14. const valueHtml = ref("<p>hello</p>");
  15. // 模拟 ajax 异步获取内容
  16. onMounted(() => {
  17. setTimeout(() => {
  18. valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
  19. }, 1500);
  20. });
  21. const toolbarConfig: any = { excludeKeys: "fullScreen" };
  22. const editorConfig = { placeholder: "请输入内容..." };
  23. // 组件销毁时,也及时销毁编辑器
  24. onBeforeUnmount(() => {
  25. const editor = editorRef.value;
  26. if (editor == null) return;
  27. editor.destroy();
  28. });
  29. const handleCreated = editor => {
  30. editorRef.value = editor; // 记录 editor 实例,重要!
  31. };
  32. </script>
  33. <template>
  34. <el-card>
  35. <template #header>
  36. <div class="card-header">
  37. <span class="font-medium">
  38. 编辑器组件,采用开源的
  39. <el-link
  40. href="https://www.wangeditor.com"
  41. target="_blank"
  42. :icon="useRenderIcon(Edit)"
  43. style="font-size: 16px; margin: 0 4px 5px"
  44. >
  45. Wangeditor
  46. </el-link>
  47. </span>
  48. </div>
  49. </template>
  50. <div class="wangeditor">
  51. <Toolbar
  52. style="border-bottom: 1px solid #ccc"
  53. :editor="editorRef"
  54. :defaultConfig="toolbarConfig"
  55. :mode="mode"
  56. />
  57. <Editor
  58. style="height: 500px; overflow-y: hidden"
  59. v-model="valueHtml"
  60. :defaultConfig="editorConfig"
  61. :mode="mode"
  62. @onCreated="handleCreated"
  63. />
  64. </div>
  65. </el-card>
  66. </template>