国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義

2024-05-06 15:35:44
字體:
來源:轉載
供稿:網友

本文為大家分享了Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義,供大家參考,具體內容如下

1.安裝

npm install vue-quill-editor --save

2.在main.js中引入

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);

3. template

<div>    <!-- 圖片上傳組件輔助-->   <el-upload    class="avatar-uploader"    :action="serverUrl"    name="img"    :headers="header"    :show-file-list="false"    :on-success="uploadSuccess"    :on-error="uploadError"    :before-upload="beforeUpload">   </el-upload>   <quill-editor    v-model="content"    ref="myQuillEditor"    :options="editorOption"    @change="onEditorChange($event)"   >   </quill-editor>  </div>

4.js

<script> const toolbarOptions = [  ['bold', 'italic', 'underline', 'strike'],    // toggled buttons  [{'header': 1}, {'header': 2}],        // custom button values  [{'list': 'ordered'}, {'list': 'bullet'}],  [{'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'],  ['clean']  ] export default {  data() {   return {    quillUpdateImg: false, // 根據圖片上傳狀態來確定是否顯示loading動畫,剛開始是false,不顯示    content: null,    editorOption: {     placeholder: '',     theme: 'snow', // or 'bubble'     modules: {      toolbar: {       container: toolbarOptions,       handlers: {        'image': function (value) {         if (value) {          // 觸發input框選擇圖片文件          document.querySelector('.avatar-uploader input').click()         } else {          this.quill.format('image', false);         }        }       }      }     }    },    serverUrl: '/manager/common/imgUpload', // 這里寫你要上傳的圖片服務器地址    header: {     // token: sessionStorage.token    } // 有的圖片服務器要求請求頭需要有token   }  },  methods: {   onEditorChange({editor, html, text}) {//內容改變事件    console.log("---內容改變事件---")    this.content = html    console.log(html)   },   // 富文本圖片上傳前   beforeUpload() {    // 顯示loading動畫    this.quillUpdateImg = true   },    uploadSuccess(res, file) {    // res為圖片服務器返回的數據    // 獲取富文本組件實例    console.log(res);    let quill = this.$refs.myQuillEditor.quill    // 如果上傳成功    if (res.code == 200 ) {     // 獲取光標所在位置     let length = quill.getSelection().index;     // 插入圖片 res.url為服務器返回的圖片地址     quill.insertEmbed(length, 'image', res.url)     // 調整光標到最后     quill.setSelection(length + 1)    } else {     this.$message.error('圖片插入失敗')    }    // loading動畫消失    this.quillUpdateImg = false   },   // 富文本圖片上傳失敗   uploadError() {    // loading動畫消失    this.quillUpdateImg = false    this.$message.error('圖片插入失敗')   }  } }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宁津县| 简阳市| 奉节县| 牟定县| 托克逊县| 彰化县| 集贤县| 会东县| 富源县| 白山市| 柳州市| 石阡县| 贵定县| 东乌珠穆沁旗| 新平| 江西省| 合肥市| 上高县| 克山县| 朝阳市| 格尔木市| 东城区| 威信县| 夏津县| 五家渠市| 思南县| 呼和浩特市| 手游| 庐江县| 汉寿县| 合山市| 修文县| 青河县| 黔东| 乌什县| 延吉市| 汉阴县| 银川市| 武宣县| 磴口县| 临洮县|