index.vue 1.8 KB

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