https://www.yuque.com/braft-editor/be/lzwpnr
https://www.yuque.com/luowenshuai/modules/idffl2

https://www.worldlink.com.cn/osdir/braft-editor.html

wangeditor

配置服务端接口

wangeditor vue 页面显示内容

在vue中使用wangEditor富文本编辑器组件的形式

第一步:我使用的npm安装 npm install wangeditor —save
创建子组件,代码如下

  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 { uploadPath } from '../../api/storage'
  11. //此处的uploadPath 为后端提供的上传图片的接口
  12. import E from 'wangeditor'
  13. export default {
  14. name: 'editoritem',
  15. data() {
  16. return {
  17. uploadPath,
  18. editor: null,
  19. info_: null
  20. }
  21. },
  22. model: {
  23. prop: 'value',
  24. event: 'change'
  25. },
  26. props: {
  27. value: {
  28. type: String,
  29. default: ''
  30. },
  31. isClear: {
  32. type: Boolean,
  33. default: false
  34. }
  35. },
  36. watch: {
  37. isClear(val) {
  38. // 触发清除文本域内容
  39. if (val) {
  40. this.editor.txt.clear()
  41. this.info_ = null
  42. }
  43. },
  44. value: function(value) {
  45. if (value !== this.editor.txt.html()) {
  46. this.editor.txt.html(this.value)
  47. }
  48. }
  49. //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  50. },
  51. mounted() {
  52. this.seteditor()
  53. this.editor.txt.html(this.value)
  54. },
  55. methods: {
  56. seteditor() {
  57. // http://192.168.2.125:8080/admin/storage/create
  58. this.editor = new E(this.$refs.toolbar, this.$refs.editor)
  59. this.editor.customConfig.uploadImgShowBase64 = true // base 64 存储图片
  60. this.editor.customConfig.uploadImgServer = uploadPath// 配置服务器端地址
  61. this.editor.customConfig.uploadImgHeaders = { }// 自定义 header
  62. this.editor.customConfig.uploadFileName = 'files' // 后端接受上传文件的参数名
  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 < result.data.length; i++) {
  110. var url = result.data[i].url
  111. insertImg(url)
  112. }
  113. }
  114. }
  115. this.editor.customConfig.onchange = (html) => {
  116. this.info_ = html // 绑定当前逐渐地值
  117. this.$emit('change', this.info_) // 将内容同步到父组件中
  118. }
  119. // 创建富文本编辑器
  120. this.editor.create()
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="css">
  126. .editor {
  127. width: 100%;
  128. margin: 0 auto;
  129. position: relative;
  130. z-index: 0;
  131. }
  132. .toolbar {
  133. border: 1px solid #ccc;
  134. }
  135. .text {
  136. border: 1px solid #ccc;
  137. min-height: 500px;
  138. }
  139. </style>

父组件中引用:

  1. <template>
  2. <div>
  3. <editor-bar v-model="goods.detail" :isClear="isClear" @change="change"></editor-bar>
  4. </div>
  5. </template>
  6. import EditorBar from './editoritem'
  7. components: { EditorBar },
  8. data() {
  9. return {
  10. isClear: false,
  11. }
  12. },
  13. methods: {
  14. change(val) {
  15. // console.log(val)
  16. this.goods.detail = val
  17. },
  18. }