欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3使用quill富文本編輯器保姆級教程(附踩坑解決)

 更新時間:2023年11月30日 11:40:30   作者:墨暖  
這篇文章主要給大家介紹了關(guān)于vue3使用quill富文本編輯器保姆級教程的相關(guān)資料,在許多網(wǎng)站和應(yīng)用程序中富文本編輯器是一種常見的工具,它使用戶能夠以直觀的方式創(chuàng)建和編輯文本內(nèi)容,需要的朋友可以參考下

vue3使用quilleditor

本文是封裝成組件使用

先放效果圖

// 安裝插件
npm install @vueup/vue-quill@alpha --save
// 局部引入
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'

先封裝組件,建立如下目錄

全部代碼如下

<template>
  <div>
  	<!-- 此處注意寫法v-model:content -->
    <QuillEditor ref="myQuillEditor"
      theme="snow"
      v-model:content="content"
      :options="data.editorOption"
      contentType="html"
      @update:content="setValue()"
    />
    <!-- 使用自定義圖片上傳 -->
    <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" />
  </div>
</template>

<script setup>
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { reactive, onMounted, ref, toRaw, watch } from 'vue'

const props = defineProps(['value'])
const emit = defineEmits(['updateValue'])
const content = ref('')
const myQuillEditor = ref()
// 通過watch監(jiān)聽回顯,筆者這邊使用v-model:content 不能正?;仫@
watch(() => props.value, (val) => {
  toRaw(myQuillEditor.value).setHTML(val)
}, { deep: true })
const fileBtn = ref()
const data = reactive({
  content: '',
  editorOption: {
    modules: {
      toolbar: [
        ['bold', 'italic', 'underline', 'strike'],
        [{ 'size': ['small', false, 'large', 'huge'] }],
        [{ 'font': [] }],
        [{ 'align': [] }],
        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
        [{ 'indent': '-1' }, { 'indent': '+1' }],
        [{ 'header': 1 }, { 'header': 2 }],
        ['image'],
        [{ 'direction': 'rtl' }],
        [{ 'color': [] }, { 'background': [] }]
      ]
    },
    placeholder: '請輸入內(nèi)容...'
  }
})
const imgHandler = (state) => {
  if (state) {
    fileBtn.value.click()
  }
}
// 拋出更改內(nèi)容,此處避免出錯直接使用文檔提供的getHTML方法
const setValue = () => {
  const text = toRaw(myQuillEditor.value).getHTML()
}
const handleUpload = (e) => {
  const files = Array.prototype.slice.call(e.target.files)
  // console.log(files, "files")
  if (!files) {
    return
  }
  const formdata = new FormData()
  formdata.append('file', files[0])
  backsite.uploadFile(formdata)  // 此處使用服務(wù)端提供上傳接口
    .then(res => {
      if (res.data.url) {
        const quill = toRaw(myQuillEditor.value).getQuill()
        const length = quill.getSelection().index
        quill.insertEmbed(length, 'image', res.data.url)
        quill.setSelection(length + 1)
      }
    })
}
// 初始化編輯器
onMounted(() => {
  const quill = toRaw(myQuillEditor.value).getQuill()
  if (myQuillEditor.value) {
    quill.getModule('toolbar').addHandler('image', imgHandler)
  }
})
</script>
<style scoped lang="scss">
// 調(diào)整樣式
:deep(.ql-editor) {
  min-height: 180px;
}
:deep(.ql-formats) {
  height: 21px;
  line-height: 21px;
}
</style>

使用

<template>
  <div class="page">
	<Editor :value="emailForm.msg" @updateValue="getMsg" />
  </div>
</template>

<script setup>
import Editor from '@/components/Editor/index.vue'

const emailForm = reactive({
  test_msg: ''
})
const getMsg = (val) => {
  emailForm.msg = val
}
</script>

本文是第二個頁面使用這個富文本編輯器有可能watch監(jiān)聽中找不到ref,如果不能正常使用可以稍微改裝下在onMounted里賦值然后在setValue里拋出就好

<template>
  <div>
    <QuillEditor ref="myQuillEditor"
      theme="snow"
      v-model:content="content"
      :options="data.editorOption"
      contentType="html"
      @update:content="setValue()"
    />
    <!-- 使用自定義圖片上傳 -->
    <input type="file" hidden accept=".jpg,.png" ref="fileBtn" @change="handleUpload" />
  </div>
</template>

<script setup>
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { reactive, onMounted, ref, toRaw, watch } from 'vue'
// import { backsite } from '@/api'

const props = defineProps(['value'])
const emit = defineEmits(['updateValue'])
const content = ref('')
const myQuillEditor = ref()
// watch(() => props.value, (val) => {
//   console.log(toRaw(myQuillEditor.value))
//   toRaw(myQuillEditor.value).setHTML(val)
// }, { deep: true })
const fileBtn = ref()
const data = reactive({
  content: '',
  editorOption: {
    modules: {
      toolbar: [
        ['bold', 'italic', 'underline', 'strike'],
        [{ 'size': ['small', false, 'large', 'huge'] }],
        [{ 'font': [] }],
        [{ 'align': [] }],
        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
        [{ 'indent': '-1' }, { 'indent': '+1' }],
        [{ 'header': 1 }, { 'header': 2 }],
        ['image'],
        [{ 'direction': 'rtl' }],
        [{ 'color': [] }, { 'background': [] }]
      ]
    },
    placeholder: '請輸入內(nèi)容...'
  }
})
const imgHandler = (state) => {
  if (state) {
    fileBtn.value.click()
  }
}
const setValue = () => {
  const text = toRaw(myQuillEditor.value).getHTML()
  emit('updateValue', text)
}
const handleUpload = (e) => {
  const files = Array.prototype.slice.call(e.target.files)
  // console.log(files, "files")
  if (!files) {
    return
  }
  const formdata = new FormData()
  formdata.append('file', files[0])
  // backsite.uploadFile(formdata)
  //   .then(res => {
  //     if (res.data.url) {
  //       const quill = toRaw(myQuillEditor.value).getQuill()
  //       const length = quill.getSelection().index
  //       // 插入圖片,res為服務(wù)器返回的圖片鏈接地址
  //       quill.insertEmbed(length, 'image', res.data.url)
  //       // 調(diào)整光標(biāo)到最后
  //       quill.setSelection(length + 1)
  //     }
  //   })
}
onMounted(() => {
  const quill = toRaw(myQuillEditor.value).getQuill()
  if (myQuillEditor.value) {
    quill.getModule('toolbar').addHandler('image', imgHandler)
  }
  toRaw(myQuillEditor.value).setHTML(props.value)
})
</script>
<style scoped lang="scss">
:deep(.ql-editor) {
  min-height: 180px;
}
:deep(.ql-formats) {
  height: 21px;
  line-height: 21px;
}
</style>

保姆級教程,有問題歡迎提出

總結(jié)

到此這篇關(guān)于vue3使用quill富文本編輯器的文章就介紹到這了,更多相關(guān)vue3使用quill富文本編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vuepress打包部署踩坑及解決

    vuepress打包部署踩坑及解決

    這篇文章主要介紹了vuepress打包部署踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 自定義vue全局組件use使用、vuex的使用詳解

    自定義vue全局組件use使用、vuex的使用詳解

    本篇文章主要介紹了自定義vue全局組件use使用、vuex的使用詳解,本文主要來講解一下怎么樣定義一個全局組件,并解釋vue.use()的原理
    2017-06-06
  • 分享Vue子組件接收父組件傳值的3種方式

    分享Vue子組件接收父組件傳值的3種方式

    這篇文章主要給大家分享的是Vue子組件接收父組件傳值的3種方式,主要通過聲明接收、接收數(shù)據(jù)的同時進(jìn)行?類型限制、接收數(shù)據(jù)的同時對?數(shù)據(jù)類型、必要性、默認(rèn)值?進(jìn)行限制相關(guān)內(nèi)容展開更多詳細(xì)的相關(guān)資料,需要的小伙伴可以參考一下
    2022-03-03
  • Vue方法與事件處理器詳解

    Vue方法與事件處理器詳解

    這篇文章主要為大家詳細(xì)介紹了Vue方法與事件處理器,,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條功能

    uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條功能

    這篇文章主要介紹了uni-app自定義導(dǎo)航欄按鈕|uniapp仿微信頂部導(dǎo)航條,需要的朋友可以參考下
    2019-11-11
  • vue3+element?Plus實現(xiàn)表格前端分頁完整示例

    vue3+element?Plus實現(xiàn)表格前端分頁完整示例

    這篇文章主要給大家介紹了關(guān)于vue3+element?Plus實現(xiàn)表格前端分頁的相關(guān)資料,雖然很多時候后端會把分頁,搜索,排序都做好,但是有些返回數(shù)據(jù)并不多的頁面,或者其他原因不能后端分頁的通常會前端處理,需要的朋友可以參考下
    2023-08-08
  • Vue中axios攔截器如何單獨配置token

    Vue中axios攔截器如何單獨配置token

    這篇文章主要介紹了Vue axios攔截器如何單獨配置token及vue axios攔截器的使用,需要的朋友可以參考下
    2019-12-12
  • vue2 全局變量的設(shè)置方法

    vue2 全局變量的設(shè)置方法

    下面小編就為大家分享一篇vue2 全局變量的設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 在vue中使用console.log無效的解決

    在vue中使用console.log無效的解決

    這篇文章主要介紹了在vue中使用console.log無效的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 使用Vue進(jìn)行數(shù)據(jù)可視化實踐分享

    使用Vue進(jìn)行數(shù)據(jù)可視化實踐分享

    在當(dāng)今的數(shù)據(jù)驅(qū)動時代,數(shù)據(jù)可視化變得越來越重要,它能夠幫助我們更直觀地理解數(shù)據(jù),從而做出更好的決策,在這篇博客中,我們將探索如何使用 Vue 和一些常見的圖表庫(如 Chart.js)來制作漂亮的數(shù)據(jù)可視化效果,需要的朋友可以參考下
    2024-10-10

最新評論