詳解Vue基於vue-quill-editor富文本編輯器使用心得


vue-quill-editor的guthub地址 ,現在市面上有很多的富文本編輯器,我個人還是非常推薦Vue自己家的vue-quill-deitor,雖然說只支持IE10+,但這種問題,帥給別人吧!

那么我們直擊正題,在vue中使用quill呢,我們需要npm進行安裝,安裝命令如下:

?
1
npm install vue-quill-editor

再安裝依賴項

?
1
npm install quill

使用:

在main.js中進行引入

?
1
2
3
4
5
6
7
import Vue from 'vue'
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
  
Vue.use(VueQuillEditor)

下面的css一定還要引用,否則編輯器將會沒有css。

在vue頁面中代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<template>
  <div class= "edit_container" >
   <quill-editor
    v-model= "content"
    ref= "myQuillEditor"
    :options= "editorOption"
    @blur= "onEditorBlur($event)" @focus= "onEditorFocus($event)"
    @change= "onEditorChange($event)" >
   </quill-editor>
   <button v-on:click= "saveHtml" >保存</button>
  </div>
</template>
 
<script>
export default {
  name: 'App' ,
  data(){
   return {
    content: `<p>hello world</p>`,
    editorOption: {}
   }
  },computed: {
   editor() {
    return this .$refs.myQuillEditor.quill;
   },
  },methods: {
   onEditorReady(editor) { // 准備編輯器
   },
   onEditorBlur(){}, // 失去焦點事件
   onEditorFocus(){}, // 獲得焦點事件
   onEditorChange(){}, // 內容改變事件
   saveHtml: function (event){
    alert( this .content);
   }
  }
}
</script>
 
<style>
#app {
  font-family: 'Avenir' , Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

其中的v-model就是我們自己的html代碼,你可以將這個html直接放到數據庫,這樣也就沒有什么問題了。如果想要禁用編輯器可以通過以下代碼:

?
1
2
3
4
onEditorFocus(val,editor){ // 富文本獲得焦點時的事件
   console.log(val); // 富文本獲得焦點時的內容
   editor.enable( false ); // 在獲取焦點的時候禁用
  }

主題設置

在vue項目中,具體引入Quill的文件中,需要使用哪種主題就寫哪個。默認是snow主題的。

?
1
2
3
4
5
6
7
8
data(){
   return {
    content: `<p>hello world</p>`,
    editorOption: {
     theme: 'snow'
    }
   }
  }

工具欄設置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
modules:{
    toolbar:[
     [ 'bold' , 'italic' , 'underline' , 'strike' ], //加粗,斜體,下划線,刪除線
     [ 'blockquote' , 'code-block' ],  //引用,代碼塊
  
  
     [{ 'header' : 1 }, { 'header' : 2 }],  // 標題,鍵值對的形式;1、2表示字體大小
     [{ 'list' : 'ordered' }, { 'list' : 'bullet' }],  //列表
     [{ 'script' : 'sub' }, { 'script' : 'super' }], // 上下標
     [{ 'indent' : '-1' }, { 'indent' : '+1' }],  // 縮進
     [{ 'direction' : 'rtl' }],    // 文本方向
  
  
     [{ 'size' : [ 'small' , false , 'large' , 'huge' ] }], // 字體大小
     [{ 'header' : [1, 2, 3, 4, 5, 6, false ] }],  //幾級標題
  
  
     [{ 'color' : [] }, { 'background' : [] }],  // 字體顏色,字體背景顏色
     [{ 'font' : [] }],  //字體
     [{ 'align' : [] }], //對齊方式
  
  
     [ 'clean' ], //清除字體樣式
     [ 'image' , 'video' ] //上傳圖片、上傳視頻
  
    ]
    },
    theme: 'snow'
   }
   }

圖片推拽上傳

需要安裝  quill-image-drop-module 模塊,那么改一下imageDrop設置為true,你就可以把你電腦上的圖片網上一坨就可以了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { quillEditor } from 'vue-quill-editor'
import * as Quill from 'quill' //引入編輯器
import { ImageDrop } from 'quill-image-drop-module' ;
Quill.register( 'modules/imageDrop' , ImageDrop);
export default {
  name: 'App' ,
  data(){
   return {
   editorOption:{
    modules:{
    imageDrop: true ,
    },
    theme: 'snow'
   }
   }
  }

那上傳文件那你就不用想了,你也許想先把圖片放上去,其實這個文件托上去就已經是個base64了,等你在前台讀數的時候直接decode就好~

圖片調整大小ImageResize

?
1
2
3
4
5
6
7
8
return {
   editorOption:{
    modules:{
        imageResize: {}
    },
    theme: 'snow'
   }
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM