当前位置:   article > 正文

Vue中使用富文本ueditor_vue3使用ueditor

vue3使用ueditor

Vue中使用富文本ueditor解决图片上传问题

学习新内容可能少不了官方文档

目前使用的是 vue-ueditor-wrap 组件它对ueditor进行了二次封装,集成秀米等第三方插件也方便

https://hc199421.gitee.io/vue-ueditor-wrap/#/home

1. 安装组件

注意 vue-ueditor-wrap v3 仅支持 Vue 3

Vue2的话还是下载

npm i vue-ueditor-wrap
2. 下载 UEditor

https://cdn.zhenghaochuan.com/p/vue-ueditor-wrap/zip/utf8-php.zip

3. 注册组件
  1. <template>
  2. <div>
  3. <vue-ueditor-wrap v-model="html" :config="ueConfig" mode="observer">
  4. </vue-ueditor-wrap>
  5. </div>
  6. </template>
  7. <script>
  8. import VueUeditorWrap from 'vue-ueditor-wrap';
  9. export default {
  10. components:{
  11. VueUeditorWrap
  12. },
  13. name: 'UeditorTxt',
  14. props: ['txt'],
  15. watch: {
  16. html(val) {
  17. this.$emit('text_content', val);
  18. },
  19. },
  20. data() {
  21. return {
  22. html: '',
  23. html_of: this.txt,
  24. dialogVisible: false,
  25. imageList: [],
  26. ueConfig: {
  27. UEDITOR_HOME_URL: '/UEditor/',
  28. toolbars: [
  29. [
  30. 'fullscreen',
  31. 'source',
  32. '|',
  33. 'undo',
  34. 'redo',
  35. '|',
  36. 'bold',
  37. 'italic',
  38. 'underline',
  39. 'fontborder',
  40. 'strikethrough',
  41. 'superscript',
  42. 'subscript',
  43. 'removeformat',
  44. 'formatmatch',
  45. 'autotypeset',
  46. 'blockquote',
  47. 'pasteplain',
  48. '|',
  49. 'forecolor',
  50. 'backcolor',
  51. 'insertorderedlist',
  52. 'insertunorderedlist',
  53. 'selectall',
  54. 'cleardoc',
  55. '|',
  56. 'rowspacingtop',
  57. 'rowspacingbottom',
  58. 'lineheight',
  59. '|',
  60. 'customstyle',
  61. 'paragraph',
  62. 'fontfamily',
  63. 'fontsize',
  64. //"|",
  65. //"directionalityltr",
  66. //"directionalityrtl",
  67. //"indent",
  68. '|',
  69. 'justifyleft',
  70. 'justifycenter',
  71. 'justifyright',
  72. 'justifyjustify',
  73. '|',
  74. //"touppercase",
  75. //"tolowercase",
  76. //"|",
  77. 'link',
  78. 'unlink',
  79. //"anchor",
  80. //"|",
  81. 'imagenone',
  82. 'imageleft',
  83. 'imageright',
  84. 'imagecenter',
  85. '|',
  86. 'simpleupload',
  87. 'insertimage',
  88. //"emotion",
  89. //"scrawl",
  90. //"insertvideo",
  91. //"music",
  92. //"attachment",
  93. //"map",
  94. //"gmap",
  95. //"insertframe",
  96. //"insertcode",
  97. //"webapp",
  98. 'pagebreak',
  99. //"template",
  100. //"background",
  101. '|',
  102. 'horizontal',
  103. //"date",
  104. //"time",
  105. 'spechars',
  106. //"snapscreen",
  107. //"wordimage",
  108. '|',
  109. 'inserttable',
  110. 'deletetable',
  111. 'insertparagraphbeforetable',
  112. 'insertrow',
  113. 'deleterow',
  114. 'insertcol',
  115. 'deletecol',
  116. 'mergecells',
  117. 'mergeright',
  118. 'mergedown',
  119. 'splittocells',
  120. 'splittorows',
  121. 'splittocols',
  122. //"charts",
  123. //"|",
  124. //"print",
  125. //"preview",
  126. //"searchreplace",
  127. //"drafts",
  128. //"help"
  129. ],
  130. ],
  131. fontfamily: [
  132. {
  133. label: '',
  134. name: 'songti',
  135. val: '宋体,SimSun',
  136. },
  137. {
  138. label: '仿宋',
  139. name: 'fangsong',
  140. val: '仿宋,FangSong',
  141. },
  142. {
  143. label: '仿宋_GB2312',
  144. name: 'fangsong',
  145. val: '仿宋_GB2312,FangSong',
  146. },
  147. {
  148. label: '',
  149. name: 'kaiti',
  150. val: '楷体,楷体_GB2312, SimKai',
  151. },
  152. {
  153. label: '',
  154. name: 'yahei',
  155. val: '微软雅黑,Microsoft YaHei',
  156. },
  157. {
  158. label: '',
  159. name: 'heiti',
  160. val: '黑体, SimHei',
  161. },
  162. {
  163. label: '',
  164. name: 'lishu',
  165. val: '隶书, SimLi',
  166. },
  167. {
  168. label: '',
  169. name: 'andaleMono',
  170. val: 'andale mono',
  171. },
  172. {
  173. label: '',
  174. name: 'arial',
  175. val: 'arial, helvetica,sans-serif',
  176. },
  177. {
  178. label: '',
  179. name: 'arialBlack',
  180. val: 'arial black,avant garde',
  181. },
  182. {
  183. label: '',
  184. name: 'comicSansMs',
  185. val: 'comic sans ms',
  186. },
  187. {
  188. label: '',
  189. name: 'impact',
  190. val: 'impact,chicago',
  191. },
  192. {
  193. label: '',
  194. name: 'timesNewRoman',
  195. val: 'times new roman',
  196. },
  197. ],
  198. autoHeightEnabled: false, // 编辑器不自动被内容撑高
  199. // 初始容器高度
  200. initialFrameHeight: 300,
  201. // 初始容器宽度
  202. initialFrameWidth: '100%',
  203. // 上传文件接口
  204. serverUrl: "http://127.0.0.1:3300/uploadsimg" ,
  205. imageActionName: 'uploadimage',
  206. imageAllowFiles: ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
  207. imageCompressBorder: 1600,
  208. imageCompressEnable: true,
  209. imageFieldName: 'file',
  210. imageInsertAlign: 'none',
  211. imageMaxSize: 20480000,
  212. imagePathFormat: '/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}',
  213. imageUrlPrefix: '',
  214. },
  215. };
  216. },
  217. created() {
  218. if (this.html_of != '') {
  219. this.html = this.html_of;
  220. }
  221. },
  222. methods: {
  223. openModule(e) {
  224. this.html = e;
  225. },
  226. },
  227. };
  228. </script>
  229. <style scoped></style>
写个自定义组件
使用组件

因为后端已经有写好图片上传接口了,但是有时候需要的请求参数或者返回参数的内容以及请求头都是不符合的,此时就是需要我们前端去自定义了

以上是我使用elementUI正常上传图片的请求及返回参数

要解决图片上传问题,

  1. 就得需要ueditor api地址为http://127.0.0.1:3300/uploadsimg而且为post请求

  1. 请求头要一致(不然浏览器容易报跨域问题)

  1. 请求参数为 file

  1. 返回的是图片绝对路径,插入的图片就需要直接拿data值

修改dialogs-》image.js文件

我直接把请求头给删了

然后修改了下返回内容只要保证this.imageList数组内容有属性URL为图片地址就行了

多选上传图片的话就没问题了

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

闽ICP备14008679号