index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template lang="html">
  2. <div class="editor">
  3. <div ref="toolbar" class="toolbar">
  4. </div>
  5. <div ref="editor" class="text">
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import E from "wangeditor";
  11. export default {
  12. name: "editoritem",
  13. data() {
  14. return {
  15. // uploadPath,
  16. editor: null,
  17. info_: null
  18. };
  19. },
  20. model: {
  21. prop: "value",
  22. event: "change"
  23. },
  24. props: {
  25. value: {
  26. type: String,
  27. default: ""
  28. },
  29. isClear: {
  30. type: Boolean,
  31. default: false
  32. }
  33. },
  34. watch: {
  35. isClear(val) {
  36. // 触发清除文本域内容
  37. if (val) {
  38. this.editor.txt.clear();
  39. this.info_ = null;
  40. }
  41. },
  42. value: function(value) {
  43. console.log(value)
  44. if (value !== this.editor.txt.html()) {
  45. this.editor.txt.html(this.value);
  46. }
  47. }
  48. //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  49. },
  50. mounted() {
  51. this.seteditor();
  52. this.editor.txt.html(this.value);
  53. },
  54. methods: {
  55. seteditor() {
  56. // http://192.168.2.125:8080/admin/storage/create
  57. this.editor = new E(this.$refs.toolbar, this.$refs.editor);
  58. this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存储图片
  59. this.editor.customConfig.uploadImgServer =
  60. "/api/admin/uploadfile"; // 配置服务器端地址
  61. this.editor.customConfig.uploadImgHeaders = {}; // 自定义 header
  62. this.editor.customConfig.uploadFileName = "file"; // 后端接受上传文件的参数名
  63. this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 将图片大小限制为 2M
  64. this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
  65. this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间
  66. // 配置菜单
  67. this.editor.customConfig.menus = [
  68. "head", // 标题
  69. "bold", // 粗体
  70. "fontSize", // 字号
  71. "fontName", // 字体
  72. "italic", // 斜体
  73. "underline", // 下划线
  74. "strikeThrough", // 删除线
  75. "foreColor", // 文字颜色
  76. "backColor", // 背景颜色
  77. "link", // 插入链接
  78. "list", // 列表
  79. "justify", // 对齐方式
  80. "quote", // 引用
  81. "emoticon", // 表情
  82. "image", // 插入图片
  83. "table", // 表格
  84. "video", // 插入视频
  85. "code", // 插入代码
  86. "undo", // 撤销
  87. "redo", // 重复
  88. "fullscreen" // 全屏
  89. ];
  90. this.editor.customConfig.uploadImgHooks = {
  91. fail: (xhr, editor, result) => {
  92. // 插入图片失败回调
  93. },
  94. success: (xhr, editor, result) => {
  95. // 图片上传成功回调
  96. },
  97. timeout: (xhr, editor) => {
  98. // 网络超时的回调
  99. },
  100. error: (xhr, editor) => {
  101. // 图片上传错误的回调
  102. },
  103. customInsert: (insertImg, result, editor) => {
  104. // 图片上传成功,插入图片的回调
  105. //result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...]
  106. // console.log(result.data[0].url)
  107. //insertImg()为插入图片的函数
  108. //循环插入图片
  109. // for (let i = 0; i < 1; i++) {
  110. // console.log(result)
  111. let url = result.data.url;
  112. console.log(result)
  113. insertImg(url);
  114. // }
  115. }
  116. };
  117. this.editor.customConfig.onchange = html => {
  118. this.info_ = html; // 绑定当前逐渐地值
  119. this.$emit("change", this.info_); // 将内容同步到父组件中
  120. };
  121. // 创建富文本编辑器
  122. this.editor.create();
  123. }
  124. }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .editor {
  129. width: 100%;
  130. margin: 0 auto;
  131. position: relative;
  132. z-index: 0;
  133. /deep/ .toolbar {
  134. border: 1px solid #ccc;
  135. }
  136. /deep/ .text {
  137. border: 1px solid #ccc;
  138. min-height: 500px;
  139. }
  140. }
  141. </style>