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

首頁 > 語言 > JavaScript > 正文

詳解wepy開發小程序踩過的坑(小結)

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

H5內嵌富文本編輯器

微信小程序沒有支持的原生富文本組件,可以通過web-view內嵌H5實現富文本編輯功能,起初使用的是wangEditor富文本編輯器,因為項目使用的是七牛云存儲,wangEditor在pc端上傳是沒有問題的,但在在移動端調用不了本地圖片,于是換了個功能強大二次開發較強的富文本編輯器vue-quill-editor,更多請參考官方文檔, 基于此對上傳圖片進行二次開發。

七牛云 + elementUi + vue-quill-editor上傳圖片和富文本

$ npm install vue-quill-editor element-ui --save
<template> <div class="editor">  <quill-editor   v-model="content"   ref="myQuillEditor"   :options="editorOption"   @focus="onEditorFocus($event)"   @change="onEditorChange($event)">   <!-- @blur="onEditorBlur($event)" -->  </quill-editor>  <!-- 文件上傳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">   </el-button>  </el-upload>  <div class="btn_box flex">   <button class="flex-1 save_draft" @click="handleCancel">取消</button>   <button class="flex-1 save_release" @click="handleSubmit" :disabled="!content">確定</button>  </div> </div></template><script>import Quill from 'quill'import api from '@/request/api'import Cookies from 'js-cookie'const DOMAIN = 'https://img.makeapoint.info/'export default { name: 'qillEditor', computed: {  editor() {   return this.$refs.myQuillEditor.quill  } }, created () {  this.$nextTick(() => {   if (this.$route.query.content) {    this.content = this.$route.query.content    this.tempRichText = this.content   }   let token = this.$route.query.currentToken   Cookies.set('currentToken_mini', token)  }) }, mounted () {  this.$refs.myQuillEditor.quill.getModule('toolbar').addHandler('image', this.imgHandler) }, data () {  return {   qnLocation: 'https://up-z2.qbox.me',   uploadData: {}, // 上傳參數   fullscreenLoading: false,   addRange: [],   uploadType: '', // 上傳的文件類型   content: '', // 提交的富文本內容   tempRichText: '', // 臨時富文本內容   editorOption: { // 自定義菜單    placeholder: "請輸入游記正文",    modules: {     toolbar: [      // ['bold', 'italic', 'underline', 'strike'],      // [{ 'header': 1 }, { 'header': 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] }], // 標題      // [{ 'font': [] }], // 字體      [{ 'color': [] }, { 'background': [] }],      [{ 'align': [] }],      ['blockquote'],      ['link', 'image'],      ['clean']     ]    }   }  } }, methods: {  handleCancel () { // 回退至小程序   window.wx.miniProgram.navigateBack({    delta: 1   })   window.wx.miniProgram.postMessage({ // 向小程序發送數據    data: this.tempRichText   })  },  handleSubmit () { // 返回小程序并提交富文本內容   window.wx.miniProgram.navigateBack({    delta: 1   })   window.wx.miniProgram.postMessage({ // 向小程序發送數據    data: this.content   })  },  // 圖片上傳前獲得數據token數據  qnUpload (file) {   this.fullscreenLoading = true   const suffix = file.name.split('.')   const ext = suffix.splice(suffix.length - 1, 1)[0]   return api.upload().then(res => {    this.uploadData = {     key: `image/${suffix.join('.')}_${new Date().getTime()}.${ext}`,     token: res.data.data    }   })  },  // 圖片上傳之前調取的函數  beforeUpload (file) {   return this.qnUpload(file)  },  // 圖片上傳成功回調插入到編輯器中  upScuccess (e, file, fileList) {   this.fullscreenLoading = false   let url = ''   url = DOMAIN + e.key   if (url != null && url.length > 0) { // 將文件上傳后的URL地址插入到編輯器文本中    let value = url    this.addRange = this.$refs.myQuillEditor.quill.getSelection()    // 調用編輯器的 insertEmbed 方法,插入URL    this.$refs.myQuillEditor.quill.insertEmbed(this.addRange !== null ? this.addRange.index : 0, this.uploadType, value, Quill.sources.USER)   }   this.$refs['upload'].clearFiles() // 插入成功后清除input的內容  },  // 點擊圖片icon觸發事件  imgHandler(state) {   this.addRange = this.$refs.myQuillEditor.quill.getSelection()   if (state) {    let fileInput = document.getElementById('imgInput')    fileInput.click() // 加一個觸發事件   }   this.uploadType = 'image'  },  // 點擊視頻icon觸發事件  // videoHandler(state) {  //  this.addRange = this.$refs.myQuillEditor.quill.getSelection()  //  if (state) {  //   let fileInput = document.getElementById('imgInput')  //   fileInput.click() // 加一個觸發事件  //  }  //  this.uploadType = 'video'  // },  // onEditorBlur(editor) {  //  this.content = html  // },  // 編輯器獲得光標  onEditorFocus(editor) {   editor.enable(true)  },  // 編輯器文本發生變化  onEditorChange({ editor, html, text }) {   this.content = html  } }}</script>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 江西省| 武清区| 合川市| 蒲江县| 凤城市| 康乐县| 昌乐县| 洱源县| 晋城| 齐齐哈尔市| 财经| 象州县| 洛南县| 宣威市| 汉川市| 防城港市| 水城县| 旺苍县| 兰溪市| 元朗区| 昌邑市| 茂名市| 通河县| 博罗县| 清镇市| 金乡县| 上犹县| 漯河市| 正阳县| 湘潭市| 清丰县| 汨罗市| 嘉义县| 横峰县| 伊金霍洛旗| 宿迁市| 喜德县| 涡阳县| 桂东县| 桐庐县| 城固县|