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

淺談vue中使用編輯器vue-quill-editor踩過(guò)的坑

 更新時(shí)間:2020年08月03日 10:11:32   作者:maggie_live  
這篇文章主要介紹了淺談vue中使用編輯器vue-quill-editor踩過(guò)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

結(jié)合vue+element-ui+vue-quill+editor二次封裝成組件

1.圖片上傳

分析原因

項(xiàng)目中使用vue-quill-editor富文本編輯器,在編輯內(nèi)容的時(shí)候,我們往往會(huì)編輯圖片,而vue-quill-editor默認(rèn)的處理方式是直接將圖片轉(zhuǎn)成base64格式,導(dǎo)致上傳的內(nèi)容十分龐大,且服務(wù)器接受post的數(shù)據(jù)的大小是有限制的,很有可能就提交失敗,造成用戶體驗(yàn)差。

引入element-ui

編輯editor.vue文件

<template>
  <div>
    <!-- 圖片上傳組件輔助-->
    <el-upload
      class="avatar-uploader"
      action=""
      accept="image/jpg, image/jpeg, image/png, image/gif"
      :http-request="upload"
      :before-upload="beforeUploadImg"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :show-file-list="false">
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
  import axios from '@/API/';
	import { quillEditor } from "vue-quill-editor";
	import COS from "cos-js-sdk-v5";
	import Upload from '@/components/Upload.vue';
	import { addQuillTitle } from '../utils/quill-title.js';

	import "quill/dist/quill.core.css";
	import "quill/dist/quill.snow.css";
	import "quill/dist/quill.bubble.css";
	
  export default {
    data() {
      return {
      }
    },
    methods: {
      // 上傳圖片前
      beforeUpload(res, file) {
      	const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg' 
		    const isLt1M = file.size / 1024 / 1024 < 1
		    if (!isJPG) {
		      this.$message.error('支持JPG、PNG格式的圖片,大小不得超過(guò)1M')
		    }
		    if (!isLt1M) {
		      this.$message.error('文件最大不得超過(guò)1M')
		    } 
		    return isJPG && isLt1M
      },
      // 上傳圖片成功
      uploadSuccess(res, file) {},
      // 上傳圖片失敗
      uploadError(res, file) {},
      // 上傳圖片處理過(guò)程
      upload(req){}
    }
  }
</script>

在editor.vue中引入vue-quill-editor

<template>
  <div>
    <!-- 圖片上傳組件輔助-->
    <el-upload
      class="avatar-uploader"
      action=""
      accept="image/jpg, image/jpeg, image/png, image/gif"
      :http-request="upload"
      :before-upload="beforeUploadImg"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :show-file-list="false">
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <quill-editor 
      class="info-editor" 
      v-model="content" 
      ref="QuillEditor" 
      :options="editorOption" 
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @ready="onEditorReady($event)">
    </quill-editor>
  </div>
</template>
<script>
  import axios from '@/API/';
	import { quillEditor } from "vue-quill-editor";
	import COS from "cos-js-sdk-v5";
	import Upload from '@/components/Upload.vue';
	import { addQuillTitle } from '../utils/quill-title.js';

	import "quill/dist/quill.core.css";
	import "quill/dist/quill.snow.css";
	import "quill/dist/quill.bubble.css";

  // 工具欄配置
  const toolbarOptions = [
	 ['bold', 'italic', 'underline', 'strike'],    // toggled buttons
	 ['blockquote', 'code-block'],

	 [{'header': 1}, {'header': 2}],        // custom button values
	 [{'list': 'ordered'}, {'list': 'bullet'}],
	 [{'script': 'sub'}, {'script': 'super'}],   // superscript/subscript
	 [{'indent': '-1'}, {'indent': '+1'}],     // outdent/indent
	 [{'direction': 'rtl'}],             // text direction

	 [{'size': ['small', false, 'large', 'huge']}], // custom dropdown
	 [{'header': [1, 2, 3, 4, 5, 6, false]}],

	 [{'color': []}, {'background': []}],     // dropdown with defaults from theme
	 [{'font': []}],
	 [{'align': []}],
	 ['link', 'image', 'video'],
	 ['clean']                     // remove formatting button
	]

  export default {
    data() {
      return {
      	editorOption: {
      		placeholder: '請(qǐng)輸入編輯內(nèi)容',
      		theme: 'snow', //主題風(fēng)格
      		modules: {
      			toolbar: {
      				container: toolbarOptions, // 工具欄
      				handlers: {
      					'image': function (value) {
      						if (value) {
      							document.querySelector('#quill-upload input').click()
      						} else {
      							this.quill.format('image', false);
      						}
      					}
      				}
      			}
      		}
      	}, // 富文本編輯器配置
      	content: '', //富文本內(nèi)容
      }
    },
    methods: {
      // 上傳圖片前
      beforeUpload(res, file) {
      	const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg' 
		    const isLt1M = file.size / 1024 / 1024 < 1
		    if (!isJPG) {
		      this.$message.error('支持JPG、PNG格式的圖片,大小不得超過(guò)1M')
		    }
		    if (!isLt1M) {
		      this.$message.error('文件最大不得超過(guò)1M')
		    } 
		    return isJPG && isLt1M
      },
      // 上傳圖片成功
      uploadSuccess(res, file) {
      	let quill = this.$refs.QuillEditor.quill;
	      let length = quill.getSelection().index;
	      quill.insertEmbed(length, 'image', url);
	      quill.setSelection(length+1)
      },
      // 上傳圖片失敗
      uploadError(res, file) {
        this.$message.error('圖片插入失敗')
      },
      // 上傳圖片處理過(guò)程
      upload(req){}
    }
  }
</script>

<style scoped>
.avatar-uploader{
	display: none;
}
</style>

2.編輯器上增加title提示

在編輯器上增加一個(gè)quill-title.js的工具欄的title的配置文件

const titleConfig = {
 'ql-bold':'加粗',
 'ql-color':'字體顏色',
 'ql-font':'字體',
 'ql-code':'插入代碼',
 'ql-italic':'斜體',
 'ql-link':'添加鏈接',
 'ql-background':'背景顏色',
 'ql-size':'字體大小',
 'ql-strike':'刪除線',
 'ql-script':'上標(biāo)/下標(biāo)',
 'ql-underline':'下劃線',
 'ql-blockquote':'引用',
 'ql-header':'標(biāo)題',
 'ql-indent':'縮進(jìn)',
 'ql-list':'列表',
 'ql-align':'文本對(duì)齊',
 'ql-direction':'文本方向',
 'ql-code-block':'代碼塊',
 'ql-formula':'公式',
 'ql-image':'圖片',
 'ql-video':'視頻',
 'ql-clean':'清除字體樣式'
};



export function addQuillTitle(){
 const oToolBar = document.querySelector('.ql-toolbar'),
    aButton = oToolBar.querySelectorAll('button'),
    aSelect = oToolBar.querySelectorAll('select'),
    aSpan= oToolBar.querySelectorAll('span');
 aButton.forEach((item)=>{
  if(item.className === 'ql-script'){
   item.value === 'sub' ? item.title = '下標(biāo)': item.title = '上標(biāo)';
  }else if(item.className === 'ql-indent'){
   item.value === '+1' ? item.title ='向右縮進(jìn)': item.title ='向左縮進(jìn)';
  }else if(item.className === 'ql-list'){
   item.value==='ordered' ? item.title='有序列表' : item.title='無(wú)序列表'
  }else if(item.className === 'ql-header'){
   item.value === '1' ? item.title = '標(biāo)題H1': item.title = '標(biāo)題H2';
  }else{
   item.title = titleConfig[item.classList[0]];
  }
 });
 aSelect.forEach((item)=>{
  if(item.className!='ql-color'&&item.className!='ql-background'){
   item.parentNode.title = titleConfig[item.classList[0]];
  }
 });
 aSpan.forEach((item)=>{
  if(item.classList[0]==='ql-color'){
   item.title = titleConfig[item.classList[0]];
  }else if(item.classList[0]==='ql-background'){
   item.title = titleConfig[item.classList[0]];
  }
 });
}

在editor.vue中引入quill-title.js

<template>
  <div>
    <!-- 圖片上傳組件輔助-->
    <el-upload
      class="avatar-uploader"
      action=""
      accept="image/jpg, image/jpeg, image/png, image/gif"
      :http-request="upload"
      :before-upload="beforeUploadImg"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :show-file-list="false">
      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <quill-editor 
      class="info-editor" 
      v-model="content" 
      ref="QuillEditor" 
      :options="editorOption" 
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @ready="onEditorReady($event)">
    </quill-editor>
  </div>
</template>
<script>
  import axios from '@/API/';
	import { quillEditor } from "vue-quill-editor";
	import COS from "cos-js-sdk-v5";
	import Upload from '@/components/Upload.vue';
	import { addQuillTitle } from '../utils/quill-title.js';

	import "quill/dist/quill.core.css";
	import "quill/dist/quill.snow.css";
	import "quill/dist/quill.bubble.css";

  // 工具欄配置
  const toolbarOptions = [
	 ['bold', 'italic', 'underline', 'strike'],    // toggled buttons
	 ['blockquote', 'code-block'],

	 [{'header': 1}, {'header': 2}],        // custom button values
	 [{'list': 'ordered'}, {'list': 'bullet'}],
	 [{'script': 'sub'}, {'script': 'super'}],   // superscript/subscript
	 [{'indent': '-1'}, {'indent': '+1'}],     // outdent/indent
	 [{'direction': 'rtl'}],             // text direction

	 [{'size': ['small', false, 'large', 'huge']}], // custom dropdown
	 [{'header': [1, 2, 3, 4, 5, 6, false]}],

	 [{'color': []}, {'background': []}],     // dropdown with defaults from theme
	 [{'font': []}],
	 [{'align': []}],
	 ['link', 'image', 'video'],
	 ['clean']                     // remove formatting button
	]

  export default {
    data() {
      return {
      	editorOption: {
      		placeholder: '請(qǐng)輸入編輯內(nèi)容',
      		theme: 'snow', //主題風(fēng)格
      		modules: {
      			toolbar: {
      				container: toolbarOptions, // 工具欄
      				handlers: {
      					'image': function (value) {
      						if (value) {
      							document.querySelector('#quill-upload input').click()
      						} else {
      							this.quill.format('image', false);
      						}
      					}
      				}
      			}
      		}
      	}, // 富文本編輯器配置
      	content: '', //富文本內(nèi)容
      }
    },
    mounted(){
	    addQuillTitle();
	  },
    methods: {
      // 上傳圖片前
      beforeUpload(res, file) {
      	const isJPG = file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/jpeg' 
		    const isLt1M = file.size / 1024 / 1024 < 1
		    if (!isJPG) {
		      this.$message.error('支持JPG、PNG格式的圖片,大小不得超過(guò)1M')
		    }
		    if (!isLt1M) {
		      this.$message.error('文件最大不得超過(guò)1M')
		    } 
		    return isJPG && isLt1M
      },
      // 上傳圖片成功
      uploadSuccess(res, file) {
      	let quill = this.$refs.QuillEditor.quill;
	      let length = quill.getSelection().index;
	      quill.insertEmbed(length, 'image', url);
	      quill.setSelection(length+1)
      },
      // 上傳圖片失敗
      uploadError(res, file) {
        this.$message.error('圖片插入失敗')
      },
      // 上傳圖片處理過(guò)程
      upload(req){}
    }
  }
</script>

<style scoped>
.avatar-uploader{
	display: none;
}
</style>

補(bǔ)充知識(shí):Vue引用quill富文本編輯器,圖片處理的兩個(gè)神器插件(quill-image-drop-module、quill-image-resize-module)的正確姿勢(shì)。(解決各種報(bào)錯(cuò))

一、前言

我在vue-quill-editor的Github認(rèn)識(shí)了這兩個(gè)插件。

quill-image-drop-module:允許粘貼圖像并將其拖放到編輯器中。

quill-image-resize-module:允許調(diào)整圖像大小。

都很實(shí)用吶!

然而DEMO不夠詳細(xì),實(shí)際引用時(shí),報(bào)了很多錯(cuò)誤。

Cannot read property 'imports' of undefined"、

Failed to mount component: template or render function not defined.、

Cannot read property 'register' of undefined。

下面說(shuō)一下正確的引用姿勢(shì)。

二、我的環(huán)境

Webpack + Vue-Cli 2.0 (vue init非simple的版本)

三、正文

1、確保你的quill富文本編輯器(不添加插件時(shí))可以正常運(yùn)行。

2、安裝NPM依賴(lài)。

cnpm install quill-image-drop-module -S

cnpm install quill-image-resize-module -S

3、在build文件夾下找到webpack.base.conf.js。

修改:

module.exports = {
 plugins: [
     new webpack.ProvidePlugin({
     // 在這兒添加下面兩行
       'window.Quill': 'quill/dist/quill.js',
       'Quill': 'quill/dist/quill.js'
    })
 ]
}

4、修改你在頁(yè)面引用的“quill富文本組件”。

修改<script>標(biāo)簽內(nèi)代碼:

  // 以前需要有下面幾行:
  import { quillEditor } from 'vue-quill-editor' //注意quillEditor必須加大括號(hào),否則報(bào)錯(cuò)。
  import "quill/dist/quill.core.css";//
  import "quill/dist/quill.snow.css";

  // 新增下面代碼:
  import resizeImage from 'quill-image-resize-module' // 調(diào)整大小組件。
  import { ImageDrop } from 'quill-image-drop-module'; // 拖動(dòng)加載圖片組件。
  Quill.register('modules/imageDrop', ImageDrop);
  Quill.register('modules/resizeImage ', resizeImage )

然后,修改頁(yè)面引用的:options="editorOption"。

editorOption: {
 modules: {
 // 新增下面
 imageDrop: true, // 拖動(dòng)加載圖片組件。
    imageResize: { //調(diào)整大小組件。
       displayStyles: {
         backgroundColor: 'black',
         border: 'none',
         color: 'white'
       },
       modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
     }
 }
}

重新 npm run dev ,插件運(yùn)行成功!

以上這篇淺談vue中使用編輯器vue-quill-editor踩過(guò)的坑就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Vue2?watch監(jiān)聽(tīng)props的值

    詳解Vue2?watch監(jiān)聽(tīng)props的值

    再次遇到監(jiān)聽(tīng)子組件收到父組件傳過(guò)來(lái)的值,如果這個(gè)值變化,頁(yè)面中的值發(fā)現(xiàn)是不會(huì)跟著同步變化的,本文給大家介紹Vue2?watch監(jiān)聽(tīng)props的值,感興趣的朋友一起看看吧
    2023-12-12
  • 基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)

    基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)

    在vue項(xiàng)目中我們經(jīng)常遇到圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于如何基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)的相關(guān)資料,文章通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能

    Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能

    這篇文章主要介紹了Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Vue2.0 UI框架ElementUI使用方法詳解

    Vue2.0 UI框架ElementUI使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue2.0 UI框架ElementUI的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • vue-router中的鉤子函數(shù)和執(zhí)行順序說(shuō)明

    vue-router中的鉤子函數(shù)和執(zhí)行順序說(shuō)明

    這篇文章主要介紹了vue-router中的鉤子函數(shù)和執(zhí)行順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Vue mixins混入使用解析

    Vue mixins混入使用解析

    如果我們?cè)诿總€(gè)組件中去重復(fù)定義這些屬性和方法會(huì)使得項(xiàng)目出現(xiàn)代碼冗余并提高了維護(hù)難度,針對(duì)這種情況官方提供了Mixins特性,這時(shí)使用Vue mixins混入有很大好處,下面就介紹下Vue mixins混入使用方法,需要的朋友參考下吧
    2023-02-02
  • Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式

    Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式

    這篇文章主要介紹了Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理

    vue實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • vue中生成條形碼(jsbarcode)和二維碼(qrcodejs2)的簡(jiǎn)單示例

    vue中生成條形碼(jsbarcode)和二維碼(qrcodejs2)的簡(jiǎn)單示例

    在vue項(xiàng)目中難免遇到有要生成條形碼或者二維碼的功能需求,下面這篇文章主要給大家介紹了關(guān)于vue中生成條形碼(jsbarcode)和二維碼(qrcodejs2)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • elementUi 中table表尾插入行的實(shí)例

    elementUi 中table表尾插入行的實(shí)例

    這篇文章主要介紹了elementUi 中table表尾插入行的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評(píng)論