[web教学] vue3利用quill富文本编辑器,保姆级教程,富文本踩坑办理

[复制链接]
查看1130 | 回复0 | 2023-8-23 11:36:38 | 显示全部楼层 |阅读模式 来自 中国北京
vue3利用quilleditor

本文是封装成组件利用

先放结果图

  1. // 安装插件
  2. npm install @vueup/vue-quill@alpha --save
  3. // 局部引入
  4. import { QuillEditor } from '@vueup/vue-quill'
  5. import '@vueup/vue-quill/dist/vue-quill.snow.css'
复制代码
先封装组件,创建如下目次

全部代码如下,
  1. <template>
  2.   <div>
  3.           <!-- 此处注意写法v-model:content -->
  4.     <QuillEditor ref="myQuillEditor"
  5.       theme="snow"
  6.       v-model:content="content"
  7.       :options="data.editorOption"
  8.       contentType="html"
  9.       @update:content="setValue()"
  10.     />
  11.     <!-- 使用自定义图片上传 -->
  12.     <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" />
  13.   </div>
  14. </template>
  15. <script setup>
  16. import { QuillEditor } from '@vueup/vue-quill'
  17. import '@vueup/vue-quill/dist/vue-quill.snow.css'
  18. import { reactive, onMounted, ref, toRaw, watch } from 'vue'
  19. const props = defineProps(['value'])
  20. const emit = defineEmits(['updateValue'])
  21. const content = ref('')
  22. const myQuillEditor = ref()
  23. // 通过watch监听回显,笔者这边使用v-model:content 不能正常回显
  24. watch(() => props.value, (val) => {
  25.   toRaw(myQuillEditor.value).setHTML(val)
  26. }, { deep: true })
  27. const fileBtn = ref()
  28. const data = reactive({
  29.   content: '',
  30.   editorOption: {
  31.     modules: {
  32.       toolbar: [
  33.         ['bold', 'italic', 'underline', 'strike'],
  34.         [{ 'size': ['small', false, 'large', 'huge'] }],
  35.         [{ 'font': [] }],
  36.         [{ 'align': [] }],
  37.         [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  38.         [{ 'indent': '-1' }, { 'indent': '+1' }],
  39.         [{ 'header': 1 }, { 'header': 2 }],
  40.         ['image'],
  41.         [{ 'direction': 'rtl' }],
  42.         [{ 'color': [] }, { 'background': [] }]
  43.       ]
  44.     },
  45.     placeholder: '请输入内容...'
  46.   }
  47. })
  48. const imgHandler = (state) => {
  49.   if (state) {
  50.     fileBtn.value.click()
  51.   }
  52. }
  53. // 抛出更改内容,此处避免出错直接使用文档提供的getHTML方法
  54. const setValue = () => {
  55.   const text = toRaw(myQuillEditor.value).getHTML()
  56. }
  57. const handleUpload = (e) => {
  58.   const files = Array.prototype.slice.call(e.target.files)
  59.   // console.log(files, "files")
  60.   if (!files) {
  61.     return
  62.   }
  63.   const formdata = new FormData()
  64.   formdata.append('file', files[0])
  65.   backsite.uploadFile(formdata)  // 此处使用服务端提供上传接口
  66.     .then(res => {
  67.       if (res.data.url) {
  68.         const quill = toRaw(myQuillEditor.value).getQuill()
  69.         const length = quill.getSelection().index
  70.         quill.insertEmbed(length, 'image', res.data.url)
  71.         quill.setSelection(length + 1)
  72.       }
  73.     })
  74. }
  75. // 初始化编辑器
  76. onMounted(() => {
  77.   const quill = toRaw(myQuillEditor.value).getQuill()
  78.   if (myQuillEditor.value) {
  79.     quill.getModule('toolbar').addHandler('image', imgHandler)
  80.   }
  81. })
  82. </script>
  83. <style scoped lang="scss">
  84. // 调整样式
  85. :deep(.ql-editor) {
  86.   min-height: 180px;
  87. }
  88. :deep(.ql-formats) {
  89.   height: 21px;
  90.   line-height: 21px;
  91. }
  92. </style>
复制代码
利用
  1. <template>
  2.   <div class="page">
  3.         <Editor :value="emailForm.msg" @updateValue="getMsg" />
  4.   </div>
  5. </template>
  6. <script setup>
  7. import Editor from '@/components/Editor/index.vue'
  8. const emailForm = reactive({
  9.   test_msg: ''
  10. })
  11. const getMsg = (val) => {
  12.   emailForm.msg = val
  13. }
  14. </script>
复制代码
本文是第二个页面利用这个富文本编辑器有大概watch监听中找不到ref,如果不能正常利用可以轻微改装下在onMounted里赋值然后在setValue里抛出就好
  1. <template>
  2.   <div>
  3.     <QuillEditor ref="myQuillEditor"
  4.       theme="snow"
  5.       v-model:content="content"
  6.       :options="data.editorOption"
  7.       contentType="html"
  8.       @update:content="setValue()"
  9.     />
  10.     <!-- 使用自定义图片上传 -->
  11.     <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" />
  12.   </div>
  13. </template>
  14. <script setup>
  15. import { QuillEditor } from '@vueup/vue-quill'
  16. import '@vueup/vue-quill/dist/vue-quill.snow.css'
  17. import { reactive, onMounted, ref, toRaw, watch } from 'vue'
  18. // import { backsite } from '@/api'
  19. const props = defineProps(['value'])
  20. const emit = defineEmits(['updateValue'])
  21. const content = ref('')
  22. const myQuillEditor = ref()
  23. // watch(() => props.value, (val) => {
  24. //   console.log(toRaw(myQuillEditor.value))
  25. //   toRaw(myQuillEditor.value).setHTML(val)
  26. // }, { deep: true })
  27. const fileBtn = ref()
  28. const data = reactive({
  29.   content: '',
  30.   editorOption: {
  31.     modules: {
  32.       toolbar: [
  33.         ['bold', 'italic', 'underline', 'strike'],
  34.         [{ 'size': ['small', false, 'large', 'huge'] }],
  35.         [{ 'font': [] }],
  36.         [{ 'align': [] }],
  37.         [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  38.         [{ 'indent': '-1' }, { 'indent': '+1' }],
  39.         [{ 'header': 1 }, { 'header': 2 }],
  40.         ['image'],
  41.         [{ 'direction': 'rtl' }],
  42.         [{ 'color': [] }, { 'background': [] }]
  43.       ]
  44.     },
  45.     placeholder: '请输入内容...'
  46.   }
  47. })
  48. const imgHandler = (state) => {
  49.   if (state) {
  50.     fileBtn.value.click()
  51.   }
  52. }
  53. const setValue = () => {
  54.   const text = toRaw(myQuillEditor.value).getHTML()
  55.   emit('updateValue', text)
  56. }
  57. const handleUpload = (e) => {
  58.   const files = Array.prototype.slice.call(e.target.files)
  59.   // console.log(files, "files")
  60.   if (!files) {
  61.     return
  62.   }
  63.   const formdata = new FormData()
  64.   formdata.append('file', files[0])
  65.   // backsite.uploadFile(formdata)
  66.   //   .then(res => {
  67.   //     if (res.data.url) {
  68.   //       const quill = toRaw(myQuillEditor.value).getQuill()
  69.   //       const length = quill.getSelection().index
  70.   //       // 插入图片,res为服务器返回的图片链接地址
  71.   //       quill.insertEmbed(length, 'image', res.data.url)
  72.   //       // 调整光标到最后
  73.   //       quill.setSelection(length + 1)
  74.   //     }
  75.   //   })
  76. }
  77. onMounted(() => {
  78.   const quill = toRaw(myQuillEditor.value).getQuill()
  79.   if (myQuillEditor.value) {
  80.     quill.getModule('toolbar').addHandler('image', imgHandler)
  81.   }
  82.   toRaw(myQuillEditor.value).setHTML(props.value)
  83. })
  84. </script>
  85. <style scoped lang="scss">
  86. :deep(.ql-editor) {
  87.   min-height: 180px;
  88. }
  89. :deep(.ql-formats) {
  90.   height: 21px;
  91.   line-height: 21px;
  92. }
  93. </style>
复制代码
保姆级教程,有题目欢迎提出

来源:https://blog.csdn.net/moanuan/article/details/128240291
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则