index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (value !== this.editor.txt.html()) {
  44. this.editor.txt.html(value);
  45. }
  46. }
  47. //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  48. },
  49. mounted() {
  50. this.seteditor();
  51. this.editor.txt.html(this.value);
  52. },
  53. methods: {
  54. getEditor(){
  55. return this.editor.txt.html();
  56. },
  57. seteditor() {
  58. // http://192.168.2.125:8080/admin/storage/create
  59. this.editor = new E(this.$refs.toolbar, this.$refs.editor);
  60. this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存储图片
  61. this.editor.customConfig.uploadImgServer =
  62. "/api/admin/uploadfile"; // 配置服务器端地址
  63. this.editor.customConfig.uploadImgHeaders = {}; // 自定义 header
  64. this.editor.customConfig.uploadFileName = "file"; // 后端接受上传文件的参数名
  65. this.editor.customConfig.uploadImgMaxSize = 5 * 1024 * 1024; // 将图片大小限制为 2M
  66. this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
  67. this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间
  68. // 配置菜单
  69. this.editor.customConfig.menus = [
  70. "head", // 标题
  71. "bold", // 粗体
  72. "fontSize", // 字号
  73. "fontName", // 字体
  74. "italic", // 斜体
  75. "underline", // 下划线
  76. "strikeThrough", // 删除线
  77. "foreColor", // 文字颜色
  78. "backColor", // 背景颜色
  79. "link", // 插入链接
  80. "list", // 列表
  81. "justify", // 对齐方式
  82. "quote", // 引用
  83. "emoticon", // 表情
  84. "image", // 插入图片
  85. "table", // 表格
  86. "video", // 插入视频
  87. "code", // 插入代码
  88. "undo", // 撤销
  89. "redo", // 重复
  90. "fullscreen" // 全屏
  91. ];
  92. this.editor.customConfig.uploadImgHooks = {
  93. fail: (xhr, editor, result) => {
  94. // 插入图片失败回调
  95. },
  96. success: (xhr, editor, result) => {
  97. // 图片上传成功回调
  98. },
  99. timeout: (xhr, editor) => {
  100. // 网络超时的回调
  101. },
  102. error: (xhr, editor) => {
  103. // 图片上传错误的回调
  104. },
  105. customInsert: (insertImg, result, editor) => {
  106. // 图片上传成功,插入图片的回调
  107. //result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...]
  108. // console.log(result.data[0].url)
  109. //insertImg()为插入图片的函数
  110. //循环插入图片
  111. // for (let i = 0; i < 1; i++) {
  112. // console.log(result)
  113. let url = result.data.url;
  114. insertImg(url);
  115. // }
  116. }
  117. };
  118. this.editor.customConfig.onchange = html => {
  119. this.info_ = html; // 绑定当前逐渐地值
  120. this.$emit("change", this.info_); // 将内容同步到父组件中
  121. };
  122. // 创建富文本编辑器
  123. this.editor.create();
  124. }
  125. }
  126. };
  127. </script>
  128. <style lang="scss" scoped>
  129. .editor {
  130. width: 100%;
  131. margin: 0 auto;
  132. position: relative;
  133. z-index: 0;
  134. /deep/ .toolbar {
  135. border: 1px solid #ccc;
  136. // position: fixed;
  137. z-index: 100000;
  138. width: 100%;
  139. background: #fff;
  140. height: 52px;
  141. }
  142. /deep/ .text {
  143. border: 1px solid #ccc;
  144. min-height: 800px;
  145. height: 800px;
  146. margin-top:20px;
  147. background-color: white;
  148. // top: 52px;
  149. }
  150. overflow-y: scroll;
  151. // .w-e-toolbar{
  152. // position: fixed;
  153. // width: 100%;
  154. // background: #fff;
  155. // height: 52px;
  156. // }
  157. }
  158. </style>