最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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: '請輸入內容...'
  }
})
const imgHandler = (state) => {
  if (state) {
    fileBtn.value.click()
  }
}
// 拋出更改內容,此處避免出錯直接使用文檔提供的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)  // 此處使用服務端提供上傳接口
    .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">
// 調整樣式
: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: '請輸入內容...'
  }
})
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為服務器返回的圖片鏈接地址
  //       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)
  }
  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>

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

總結

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

相關文章

  • 詳解Vue的computed(計算屬性)使用實例之TodoList

    詳解Vue的computed(計算屬性)使用實例之TodoList

    本篇文章主要介紹了詳解Vue的computed(計算屬性)使用實例之TodoList,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • vue中報錯“error‘xxx‘?is?defined?but?never?used”問題及解決

    vue中報錯“error‘xxx‘?is?defined?but?never?used”問題及解決

    介紹了兩種解決代碼導入問題的方法:單一代碼解決和全局解決,第一種方法是在代碼前面添加特定代碼并保存;第二種方法是在package.json中添加代碼后重啟項目,這些方法可以有效解決導包錯誤提示,希望對大家有幫助
    2024-10-10
  • 詳解如何寫出一個利于擴展的vue路由配置

    詳解如何寫出一個利于擴展的vue路由配置

    這篇文章主要介紹了詳解如何寫出一個利于擴展的vue路由配置,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Vue.js實現表格動態(tài)增加刪除的方法(附源碼下載)

    Vue.js實現表格動態(tài)增加刪除的方法(附源碼下載)

    Vue.js通過簡潔的API提供高效的數據綁定和靈活的組件系統(tǒng)。在前端紛繁復雜的生態(tài)中,Vue.js有幸受到一定程度的關注,下面這篇文章主要給介紹了Vue.js實現表格動態(tài)增加刪除的方法實例,文末提供了源碼下載,需要的朋友可以參考借鑒。
    2017-01-01
  • Vue中El-table列順序一步步實現默認顯示功能

    Vue中El-table列順序一步步實現默認顯示功能

    這篇文章給大家介紹Vue中El-table列順序的秘密:一步步實現默認顯示功能,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2026-05-05
  • 使用Vue-cli 3.0搭建Vue項目的方法

    使用Vue-cli 3.0搭建Vue項目的方法

    這篇文章主要介紹了使用Vue-cli 3.0搭建Vue項目的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Vue + Webpack + Vue-loader學習教程之功能介紹篇

    Vue + Webpack + Vue-loader學習教程之功能介紹篇

    這篇文章主要介紹了關于Vue + Webpack + Vue-loader功能介紹的相關資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • vue實現商品加減計算總價的實例代碼

    vue實現商品加減計算總價的實例代碼

    這篇文章主要介紹了vue實現商品加減計算總價的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 關于vue-router的使用及實現原理

    關于vue-router的使用及實現原理

    這篇文章主要介紹了關于vue-router的使用及實現原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Vue實現Word/Excel/PDF文件預覽的詳細步驟

    Vue實現Word/Excel/PDF文件預覽的詳細步驟

    vue-office是一款專門為 Vue 設計的辦公文檔預覽組件庫,支持 ??Word(.docx),Excel(.xlsx/.xls),PDF?? 等主流格式,下面我們就來看看具體實現步驟吧
    2025-07-07

最新評論

邵阳市| 大港区| 伊川县| 铜梁县| 黄平县| 建湖县| 唐山市| 赤壁市| 赤壁市| 五寨县| 象山县| 习水县| 阳谷县| 阳谷县| 伽师县| 冀州市| 会同县| 庐江县| 长岛县| 旌德县| 周宁县| 永安市| 兴城市| 巨鹿县| 宽甸| 涟水县| 宁化县| 韶关市| 宜州市| 大丰市| 镇宁| 博湖县| 蓝田县| 洞头县| 青海省| 二手房| 秭归县| 明光市| 玉龙| 焦作市| 信宜市|