亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue項目中quill-editor帶樣式編輯器的使用方法

 更新時間:2017年08月08日 11:42:16   作者:嘉爺  
這篇文章主要介紹了Vue項目中quill-editor帶樣式編輯器的使用方法,可以更改插入圖片和視頻,具有一定的參考價值,感興趣的小伙伴們可以參考一下

vue-quill-editor默認(rèn)插入圖片是直接將圖片轉(zhuǎn)為base64再放入內(nèi)容中,如果圖片比較大的話,富文本的內(nèi)容就會很大。

插入視頻是直接彈框輸入URL地址,某些需求下我們需要讓用戶去本地選擇自己的視頻,我們可以通過vue-quill-editor內(nèi)部的某些方法進行更改

該方法使用了 element-ui 和 文件上傳七牛

一、npm 安裝 vue-quill-editor

二、在main.js中引入

import VueQuillEditor from 'vue-quill-editor'
Vue.use(VueQuillEditor)

 

HTML部分:為編輯器綁定各個API事件,定義一個隱藏的input框,用于點擊圖片或者視頻圖標(biāo)上傳文件

<template>
 <div>
 <!-- quill-editor插件標(biāo)簽 分別綁定各個事件-->
 <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
 @change="onEditorChange($event)">
 </quill-editor>
 <div class="limit">當(dāng)前已輸入 <span>{{nowLength}}</span> 個字符,您還可以輸入 <span>{{SurplusLength}}</span> 個字符。</div>
 <!-- 文件上傳input 將它隱藏-->
 <el-upload class="upload-demo" :action="qnLocation" :before-upload='beforeUpload' :data="uploadData" :on-success='upScuccess'
 ref="upload" style="display:none">
 <el-button size="small" type="primary" id="imgInput" v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="插入中,請稍候">點擊上傳</el-button>
 </el-upload>
 </el-table>
 </div>
</template>

CSS部分:

.quill-editor {
 height: 745px;

 .ql-container {
 height: 680px;
 }
}

.limit {
 height: 30px;
 border: 1px solid #ccc;
 line-height: 30px;
 text-align: right;

 span {
 color: #ee2a7b;
 }
}

.ql-snow .ql-editor img {
 max-width: 480px;
}

.ql-editor .ql-video {
 max-width: 480px;
}

 JS部分:

import Vue from 'util/vueExt'
import { Component } from 'vue-property-decorator'
import * as Template from './editor.vue'
import * as Quill from 'quill' // 引入編輯器

const STATICDOMAIN = '//ss.yidejia.com/'
const STATVIDEO = '//flv.yidejia.com/'

@Component({
 mixins: [Template]
})
export default class Editor extends Vue {
 content = '' // 文章內(nèi)容
 editorOption = {} // 編輯器選項
 addRange: any = new Array()
 uploadData = {}
 photoUrl = ''  // 上傳圖片地址
 uploadType = '' // 上傳的文件類型(圖片、視頻)
 fullscreenLoading = false

 $refs: {
 myQuillEditor: any,
 imgInput: any
 }

 get nowLength() {
 return this.content.length
 }

 get SurplusLength() { // 計算屬性 獲得當(dāng)前輸入字符長度
 let num = 10000 - Number(this.content.length)
 if (num > 0) {
 return num
 } else {
 return 0
 }
 }

 // 上傳七牛的actiond地址
 get qnLocation() {
 if (location.protocol === 'http:') {
 return 'http://up-z0.qiniu.com'
 }
 return 'https://up-z0.qbox.me'
 }

 // 圖片上傳前獲得數(shù)據(jù)token數(shù)據(jù)
 qnUpload(file) {
 this.fullscreenLoading = true
 const suffix = file.name.split('.')
 const ext = suffix.splice(suffix.length - 1, 1)[0]
 console.log(this.uploadType)
 if (this.uploadType === 'image') { // 如果是點擊插入圖片
 return this.api.getQNToken().then(res => {
 this.uploadData = {
  key: `image/${suffix.join('.')}_${new Date().getTime()}.${ext}`,
  token: res
 }
 })
 } else if (this.uploadType === 'video') { // 如果是點擊插入視頻
 return this.api.getVideoQNToken().then(res => {
 this.uploadData = {
  key: `video/${suffix.join('.')}_${new Date().getTime()}.${ext}`,
  token: res
 }
 })
 }
 }

 // 圖片上傳之前調(diào)取的函數(shù)
 beforeUpload(file) {
 return this.qnUpload(file)
 }

 // 圖片上傳成功回調(diào) 插入到編輯器中
 upScuccess(e, file, fileList) {
 this.fullscreenLoading = false
 let vm = this
 let url = ''
 if (this.uploadType === 'image') { // 獲得文件上傳后的URL地址
 url = STATICDOMAIN + e.key
 } else if (this.uploadType === 'video') {
 url = STATVIDEO + e.key
 }
 if (url != null && url.length > 0) { // 將文件上傳后的URL地址插入到編輯器文本中
 let value = url
 vm.addRange = vm.$refs.myQuillEditor.quill.getSelection()
 value = value.indexOf('http') !== -1 ? value : 'http:' + value
 vm.$refs.myQuillEditor.quill.insertEmbed(vm.addRange !== null ? vm.addRange.index : 0, vm.uploadType, value, Quill.sources.USER) // 調(diào)用編輯器的 insertEmbed 方法,插入URL
 } else {
 (<any>this).$message.error(`${vm.uploadType}插入失敗`)
 }
 this.$refs['upload'].clearFiles() // 插入成功后清除input的內(nèi)容
 }

 // 點擊圖片ICON觸發(fā)事件
 imgHandler(state) {
 this.addRange = this.$refs.myQuillEditor.quill.getSelection()
 if (state) {
 let fileInput = document.getElementById('imgInput')
 fileInput.click() // 加一個觸發(fā)事件
 }
 this.uploadType = 'image'
 }

 // 點擊視頻ICON觸發(fā)事件
 videoHandler(state) {
 this.addRange = this.$refs.myQuillEditor.quill.getSelection()
 if (state) {
 let fileInput = document.getElementById('imgInput')
 fileInput.click() // 加一個觸發(fā)事件
 }
 this.uploadType = 'video'
 }

 // 編輯器光標(biāo)離開 將編輯器內(nèi)容發(fā)射給父組件
 onEditorBlur(editor) {
 this.$emit('getValue', this.content)
 }

 // 編輯器獲得光標(biāo)
 onEditorFocus(editor) {
 editor.enable(true) // 實現(xiàn)達到上限字符可刪除
 }

 // 編輯器文本發(fā)生變化
 onEditorChange({ editor, html, text }) {
 let textLength = text.length
 if (textLength > 10000) {
 (<any>this).$message.error('最多輸入10000個字符')
 editor.enable(false)
 }
 this.$emit('getValue', this.content)
 }

 // 清除編輯器內(nèi)容
 callMethod() {
 this.content = ''
 }

 // 頁面加載后執(zhí)行 為編輯器的圖片圖標(biāo)和視頻圖標(biāo)綁定點擊事件
 mounted() {
 // 為圖片ICON綁定事件 getModule 為編輯器的內(nèi)部屬性
 this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('image', this.imgHandler)
 this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('video', this.videoHandler) // 為視頻ICON綁定事件
 }
}

相關(guān)參考鏈接:

vue-quill-editor實現(xiàn)圖片上傳功能

vue-quill-editor API文檔地址

本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。

關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue如何將字符串變?yōu)閿?shù)組

    vue如何將字符串變?yōu)閿?shù)組

    這篇文章主要介紹了vue如何將字符串變?yōu)閿?shù)組問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue3基礎(chǔ)篇之常用的循環(huán)示例詳解

    Vue3基礎(chǔ)篇之常用的循環(huán)示例詳解

    filter?方法會創(chuàng)建一個新的數(shù)組,其中包含滿足指定條件的所有元素,這個方法非常適合循環(huán)遍歷數(shù)組并根據(jù)特定條件過濾元素的情況,這篇文章主要介紹了Vue3基礎(chǔ)[常用的循環(huán)],需要的朋友可以參考下
    2024-01-01
  • Vue3 構(gòu)建 Web Components使用詳解

    Vue3 構(gòu)建 Web Components使用詳解

    這篇文章主要為大家介紹了Vue3 構(gòu)建 Web Components使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • vue3中$refs的基本使用方法

    vue3中$refs的基本使用方法

    在Vue中一般很少會用到直接操作DOM,但不可避免有時候需要用到,這時我們可以通過ref和$refs這兩個來實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue3中$refs的基本使用方法,需要的朋友可以參考下
    2022-03-03
  • vue實現(xiàn)伸縮菜單功能

    vue實現(xiàn)伸縮菜單功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)伸縮菜單功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 詳解 vue.js用法和特性

    詳解 vue.js用法和特性

    Vue.js目前已經(jīng)更新到2.x,功能和語法上有一定升級和修改,本文首先介紹基礎(chǔ)內(nèi)容。感興趣的朋友一起看看吧
    2017-10-10
  • Vue中如何進行數(shù)據(jù)響應(yīng)式更新

    Vue中如何進行數(shù)據(jù)響應(yīng)式更新

    Vue是一款流行的JavaScript框架,它提供了數(shù)據(jù)響應(yīng)式更新的能力,可以讓我們輕松地更新數(shù)據(jù),并自動更新視圖,本文將介紹Vue中如何進行數(shù)據(jù)響應(yīng)式更新,包括使用Vue的響應(yīng)式系統(tǒng)、使用計算屬性和使用Vue的watcher,需要的朋友可以參考下
    2023-06-06
  • 基于Vue 擼一個指令實現(xiàn)拖拽功能

    基于Vue 擼一個指令實現(xiàn)拖拽功能

    這篇文章主要介紹了Vue 指令實現(xiàn)拖拽功能,實現(xiàn)原理很簡單,文中通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • vue 的 Render 函數(shù)

    vue 的 Render 函數(shù)

    Vue 推薦在絕大多數(shù)情況下使用模板來創(chuàng)建你的 HTML。然而在一些場景中,你真的需要 JavaScript 的完全編程的能力。這時你可以用渲染函數(shù),它比模板更接近編譯器。下面就和小編一起來學(xué)習(xí)下面文章內(nèi)容吧
    2021-09-09
  • Vue3 之 Vue 事件處理指南

    Vue3 之 Vue 事件處理指南

    Vue事件處理是每個Vue項目的必要方面。 它用于捕獲用戶輸入,共享數(shù)據(jù)以及許多其他創(chuàng)造性方式。在本文中,會介紹基礎(chǔ)知識,并提供一些用于處理事件的代碼示例。需要的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09

最新評論