• vue-quill-editor 上传图片 先传到服务器 在显示出来


    html

    1. <div :class="prefixCls">
    2. <quill-editor
    3. v-show="visible"
    4. @focus="focus($event)"
    5. v-model="queryParam.content"
    6. ref="myQuillEditor"
    7. :options="editorOption"
    8. @change="onEditorChange($event)">
    9. quill-editor>
    10. <input
    11. type="file"
    12. accept=".png,.jpg,.jpeg"
    13. @change="change"
    14. id="upload"
    15. style="display:none"
    16. >
    17. div>

    script

    1. import 'quill/dist/quill.core.css'
    2. import 'quill/dist/quill.snow.css'
    3. import 'quill/dist/quill.bubble.css'
    4. import { quillEditor, Quill } from 'vue-quill-editor'
    5. export default {
    6. name: 'QuillEditor',
    7. components: {
    8. quillEditor
    9. },
    10. props: {
    11. prefixCls: {
    12. type: String,
    13. default: 'ant-editor-quill'
    14. },
    15. // 表单校验用字段
    16. // eslint-disable-next-line
    17. value: {
    18. type: String
    19. }
    20. },
    21. data () {
    22. return {
    23. // 标题是否禁用
    24. Title: false,
    25. // 编号是否禁用
    26. umber: false,
    27. submitd: true,
    28. resetd: true,
    29. visible: false,
    30. content: null,
    31. editorOption: {
    32. placeholder: '编辑文章内容',
    33. theme: 'snow',
    34. modules: {
    35. ImageExtend: {
    36. loading: true,
    37. name: 'pictureFile',
    38. size: 6,
    39. start: () => {},
    40. end: () => {},
    41. error: () => {},
    42. change: (xhr, formData) => {}
    43. },
    44. toolbar: {
    45. container: container,
    46. // 拦截
    47. handlers: {
    48. image: function (value) {
    49. if (value) {
    50. document.querySelector('#upload').click()// 劫持原来的图片点击按钮事件
    51. }
    52. }
    53. }
    54. }
    55. }
    56. }
    57. }
    58. },
    59. computed: {
    60. ...mapGetters(['department'])
    61. },
    62. methods: {
    63. login () {
    64. this.visible = true
    65. this.submitd = true
    66. this.resetd = true
    67. },
    68. // 获取焦点事件
    69. focus (event) {
    70. // 设置富文本编辑器不可编辑
    71. if (this.umber === true) {
    72. event.enable(false)
    73. }
    74. },
    75. show (e) {
    76. this.visible = true
    77. this.queryParam.title = e.title
    78. this.queryParam.number = e.number
    79. this.queryParam.content = e.content
    80. this.submitd = false
    81. this.resetd = false
    82. this.umber = true
    83. this.Title = true
    84. },
    85. onEditorChange ({ quill, html, text }) {
    86. this.queryParam.content = html
    87. },
    88. submit () {
    89. addMessageList({
    90. title: this.queryParam.title,
    91. content: this.queryParam.content,
    92. number: this.queryParam.number,
    93. department: this.department
    94. }).then(res => {
    95. this.$message.success('上传成功')
    96. this.visible = false
    97. this.queryParam.content = ''
    98. }).catch((err) => {
    99. this.$message.error(err.msg)
    100. })
    101. },
    102. // 拦截
    103. change (e) {
    104. const file = e.target.files[0]
    105. const formData = new FormData()
    106. // console.log(formData)
    107. formData.append('userfile', file)
    108. messageLoad(formData)
    109. .then((res) => {
    110. const quill = this.$refs.myQuillEditor.quill
    111. if (res === '上传成功') {
    112. const length = quill.getSelection().index // 光标位置
    113. // 插入图片 图片地址
    114. const imgurl = `http://网址:端口号/messagefiles/` + e.target.files[0].name
    115. quill.insertEmbed(length, 'image', imgurl)
    116. // 调整光标到最后
    117. quill.setSelection(length + 1) // 光标后移一位
    118. }
    119. })
    120. .catch((err) => {
    121. console.error(err)
    122. })
    123. },
    124. handleCancel () {
    125. this.visible = false
    126. this.queryParam.title = ''
    127. this.queryParam.number = ''
    128. this.queryParam.content = ''
    129. this.umber = false
    130. this.Title = false
    131. },
    132. // 重置
    133. reset () {
    134. this.queryParam.title = ''
    135. this.queryParam.number = ''
    136. this.queryParam.content = ''
    137. this.umber = false
    138. this.Title = false
    139. }
    140. }
    141. }

    最最主要的代码是拦截

    1. // 拦截
    2. change (e) {
    3. const file = e.target.files[0]
    4. const formData = new FormData()
    5. formData.append('userfile', file)
    6. messageLoad(formData)
    7. .then((res) => {
    8. const quill = this.$refs.myQuillEditor.quill
    9. if (res === '上传成功') {
    10. const length = quill.getSelection().index // 光标位置
    11. // 插入图片 图片地址
    12. const imgurl = `http://网址:端口号/messagefiles/` + e.target.files[0].name
    13. // const imgurl = window.location.protocol + '//' + window.location.host + '/messagefiles/' + e.target.files[0].name
    14. quill.insertEmbed(length, 'image', imgurl)
    15. // 调整光标到最后
    16. quill.setSelection(length + 1) // 光标后移一位
    17. }
    18. })
    19. .catch((err) => {
    20. console.error(err)
    21. })
    22. },

  • 相关阅读:
    成功上岸,刚转行自学Python的小姑娘,每个月入1W+......
    Linux 中的程序部署
    信息学奥赛一本通:1164:digit函数
    Grok AI 是什么?
    嘴蠢到爆表的代码:我不是写代码,我是写笑话
    【Python实战因果推断】2_因果效应异质性2
    bazel远程缓存(Remote Cache)
    RabbitMQ 学习(五)-- 死信队列
    Windows下定时下载Linux服务器的数据库备份文件(pscp+bat脚本+定时任务)
    【JVM笔记】同步控制字节码指令
  • 原文地址:https://blog.csdn.net/weixin_63199745/article/details/132886768