当前位置:   article > 正文

vue3使用wangEditor富文本编辑器_vue3 wangeditor

vue3 wangeditor

1、安装依赖

npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue@next --save

2、引入

  1. import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  2. import { IEditorConfig } from "@wangeditor/editor";
  3. import "@wangeditor/editor/dist/css/style.css";

注意:记得引入css文件,因为官网和部分教程都没写这个,导致样式错位,找了好久才找到

3、页面使用wangEditor

  1. <div style="border: 1px solid #ccc">
  2. <Toolbar
  3. style="border-bottom: 1px solid #ccc"
  4. :editor="editorRef"
  5. :defaultConfig="toolbarConfig"
  6. :mode="mode"
  7. />
  8. <Editor
  9. style="height: 500px; overflow-y: hidden"
  10. v-model="valueHtml"
  11. :defaultConfig="editorConfig"
  12. :mode="mode"
  13. @onCreated="handleCreated"
  14. />
  15. </div>

截图:

 4、设置富文本配置

  1. const mode = "default";
  2. // 编辑器实例,必须用 shallowRef
  3. const editorRef = shallowRef();
  4. // 内容 HTML
  5. const valueHtml = ref("");
  6. const toolbarConfig = {};
  7. const editorConfig: Partial<IEditorConfig> = {
  8. placeholder: "请输入内容...",
  9. MENU_CONF: {}
  10. };
  11. // 组件销毁时,也及时销毁编辑器
  12. onBeforeUnmount(() => {
  13. const editor = editorRef.value;
  14. if (editor == null) return;
  15. editor.destroy();
  16. });
  17. onBeforeMount(() => {
  18. //上传图片
  19. editorConfig.MENU_CONF["uploadImage"] = {
  20. server: import.meta.env.VITE_API_BASE_URL + "/file/uploadEditor",
  21. fieldName: "file",
  22. headers: { Authorization: "Bearer " + sessionStorage.getItem("token") },
  23. maxFileSize: 10 * 1024 * 1024,
  24. base64LimitSize: 5 * 1024,
  25. maxNumberOfFiles: 1,
  26. allowedFileTypes: ["image/*"],
  27. customInsert(res, insertFn) {
  28. console.log("customInsert", res);
  29. const imgInfo = res.data[0] || {};
  30. const { url, alt, href } = imgInfo;
  31. if (!url) throw new Error(`Image url is empty`);
  32. console.log("Your image url ", url);
  33. insertFn(url, alt, href);
  34. }
  35. // onBeforeUpload(file) {
  36. // console.log("onBeforeUpload", file);
  37. // return file; // will upload this file
  38. // // return false // prevent upload
  39. // },
  40. // onProgress(progress) {
  41. // console.log("onProgress", progress);
  42. // },
  43. // onSuccess(file, res) {
  44. // console.log("onSuccess", file, res);
  45. // },
  46. // onFailed(file, res) {
  47. // alert(res.message);
  48. // console.log("onFailed", file, res);
  49. // },
  50. // onError(file, err, res) {
  51. // console.error("onError", file, err.message, res);
  52. // }
  53. };
  54. //插入视频
  55. editorConfig.MENU_CONF["insertVideo"] = {
  56. onInsertedVideo(videoNode) {
  57. console.log("inserted video", videoNode);
  58. }
  59. };
  60. //上传视频
  61. editorConfig.MENU_CONF["uploadVideo"] = {
  62. server: import.meta.env.VITE_API_BASE_URL + "/file/uploadEditor",
  63. fieldName: "file",
  64. allowedFileTypes: ["video/*"],
  65. headers: { Authorization: "Bearer " + sessionStorage.getItem("token") },
  66. maxFileSize: 100 * 1024 * 1024,
  67. customInsert(res, insertFn) {
  68. const videoInfo = res.data[0] || {};
  69. const { url } = videoInfo;
  70. if (!url) throw new Error(`视频的url为空`);
  71. insertFn(url + "#dsffdsfsf");
  72. }
  73. // onBeforeUpload(file) {
  74. // console.log("onBeforeUpload", file);
  75. // return file; // will upload this file
  76. // // return false // prevent upload
  77. // },
  78. // onProgress(progress) {
  79. // console.log("onProgress", progress);
  80. // },
  81. // onSuccess(file, res) {
  82. // console.log("onSuccess", file, res);
  83. // },
  84. // onFailed(file, res) {
  85. // alert(res.message);
  86. // console.log("onFailed", file, res);
  87. // },
  88. // onError(file, err, res) {
  89. // alert(err.message);
  90. // console.error("onError", file, err, res);
  91. // }
  92. };
  93. });
  94. const handleCreated = editor => {
  95. editorRef.value = editor; // 记录 editor 实例,重要!
  96. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/634986
推荐阅读
相关标签
  

闽ICP备14008679号