index.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div>
  3. <div id="editor"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import wangEditor from "wangeditor";
  8. import { getToken } from "@/utils/auth";
  9. import { publicFileSaveAPI } from "@/api/common";
  10. import { publicFileGetUrl } from "@/api/common";
  11. export default {
  12. name: "WangEditor",
  13. props: {
  14. value: {
  15. default: "",
  16. type: String,
  17. },
  18. },
  19. data() {
  20. return {
  21. editor: null,
  22. };
  23. },
  24. watch: {
  25. value: "getdictionarylist", // 渲染数据的方法
  26. },
  27. methods: {
  28. initEditor() {
  29. this.editor = new wangEditor(`#editor`);
  30. // 配置 onchange 回调函数,将数据同步到 提交表单 中
  31. this.editor.config.onchange = (newHtml) => {
  32. this.$emit("input", newHtml);
  33. };
  34. this.editor.config.placeholder = "请输入回答";
  35. // 编辑器不需要的菜单
  36. this.editor.config.excludeMenus = ["emoticon", "video", "code"];
  37. this.editor.config.showLinkImg = false; //关闭网络路径图片方式
  38. this.editor.config.customUploadImg = async (files, insert) => {
  39. for (let item of files) {
  40. let res = await publicFileSaveAPI(
  41. { file: item },
  42. {
  43. Authorization: "Bearer " + getToken(),
  44. "x-zz-timestamp": new Date().valueOf(),
  45. }
  46. );
  47. let url = publicFileGetUrl + res.data.fileName;
  48. // 在编辑器插入图片
  49. insert(url);
  50. }
  51. };
  52. this.editor.config.zIndex = 0;
  53. // 创建编辑器
  54. this.editor.create();
  55. },
  56. setContent(val) {
  57. this.editor.txt.html(val);
  58. },
  59. getdictionarylist() {
  60. // this.editor.txt.html(this.value);
  61. },
  62. },
  63. mounted() {
  64. this.initEditor();
  65. },
  66. beforeDestroy() {
  67. // 销毁编辑器
  68. this.editor.destroy();
  69. this.editor = null;
  70. },
  71. };
  72. </script>
  73. <style>
  74. </style>