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

詳解wepy開(kāi)發(fā)小程序踩過(guò)的坑(小結(jié))

 更新時(shí)間:2019年05月22日 09:46:12   作者:LenGxin  
這篇文章主要介紹了詳解wepy開(kāi)發(fā)小程序踩過(guò)的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

H5內(nèi)嵌富文本編輯器

微信小程序沒(méi)有支持的原生富文本組件,可以通過(guò)web-view內(nèi)嵌H5實(shí)現(xiàn)富文本編輯功能,起初使用的是wangEditor富文本編輯器,因?yàn)轫?xiàng)目使用的是七牛云存儲(chǔ),wangEditor在pc端上傳是沒(méi)有問(wèn)題的,但在在移動(dòng)端調(diào)用不了本地圖片,于是換了個(gè)功能強(qiáng)大二次開(kāi)發(fā)較強(qiáng)的富文本編輯器vue-quill-editor,更多請(qǐng)參考官方文檔, 基于此對(duì)上傳圖片進(jìn)行二次開(kāi)發(fā)。

七牛云 + 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: {}, // 上傳參數(shù)
   fullscreenLoading: false,
   addRange: [],
   uploadType: '', // 上傳的文件類(lèi)型
   content: '', // 提交的富文本內(nèi)容
   tempRichText: '', // 臨時(shí)富文本內(nèi)容
   editorOption: { // 自定義菜單
    placeholder: "請(qǐng)輸入游記正文",
    modules: {
     toolbar: [
      // ['bold', 'italic', 'underline', 'strike'],
      // [{ 'header': 1 }, { 'header': 2 }],
      [{ 'list': 'ordered' }, { 'list': 'bullet' }],
      // [{ 'script': 'sub' }, { 'script': 'super' }],
      // [{ 'indent': '-1' }, { 'indent': '+1' }], // 縮進(jìn)
      // [{ 'direction': 'rtl' }], // 反向
      // [{ 'size': ['small', false, 'large', 'huge'] }], // 字體大小
      // [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 標(biāo)題
      // [{ 'font': [] }], // 字體
      [{ 'color': [] }, { 'background': [] }],
      [{ 'align': [] }],
      ['blockquote'],
      ['link', 'image'],
      ['clean']
     ]
    }
   }
  }
 },
 methods: {
  handleCancel () { // 回退至小程序
   window.wx.miniProgram.navigateBack({
    delta: 1
   })
   window.wx.miniProgram.postMessage({ // 向小程序發(fā)送數(shù)據(jù)
    data: this.tempRichText
   })
  },
  handleSubmit () { // 返回小程序并提交富文本內(nèi)容
   window.wx.miniProgram.navigateBack({
    delta: 1
   })
   window.wx.miniProgram.postMessage({ // 向小程序發(fā)送數(shù)據(jù)
    data: this.content
   })
  },
  // 圖片上傳前獲得數(shù)據(jù)token數(shù)據(jù)
  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
    }
   })
  },
  // 圖片上傳之前調(diào)取的函數(shù)
  beforeUpload (file) {
   return this.qnUpload(file)
  },
  // 圖片上傳成功回調(diào)插入到編輯器中
  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()
    // 調(diào)用編輯器的 insertEmbed 方法,插入U(xiǎn)RL
    this.$refs.myQuillEditor.quill.insertEmbed(this.addRange !== null ? this.addRange.index : 0, this.uploadType, value, Quill.sources.USER)
   }
   this.$refs['upload'].clearFiles() // 插入成功后清除input的內(nèi)容
  },
  // 點(diǎn)擊圖片icon觸發(fā)事件
  imgHandler(state) {
   this.addRange = this.$refs.myQuillEditor.quill.getSelection()
   if (state) {
    let fileInput = document.getElementById('imgInput')
    fileInput.click() // 加一個(gè)觸發(fā)事件
   }
   this.uploadType = 'image'
  },
  // 點(diǎn)擊視頻icon觸發(fā)事件
  // videoHandler(state) {
  //  this.addRange = this.$refs.myQuillEditor.quill.getSelection()
  //  if (state) {
  //   let fileInput = document.getElementById('imgInput')
  //   fileInput.click() // 加一個(gè)觸發(fā)事件
  //  }
  //  this.uploadType = 'video'
  // },
  // onEditorBlur(editor) {
  //  this.content = html
  // },
  // 編輯器獲得光標(biāo)
  onEditorFocus(editor) {
   editor.enable(true)
  },
  // 編輯器文本發(fā)生變化
  onEditorChange({ editor, html, text }) {
   this.content = html
  }
 }
}
</script>

<style lang="less">
.quill-editor {
 .ql-container {
  min-height: 50vh;
 }
}
.ql-editor img {
 width: 100%;
 height: 200px;
}
</style>

<style lang="less" scoped>
.editor {
 width: 100%;
 height: 100vh;
 .flex {
  display: flex;
 }
 .flex-1 {
  flex: 1;
 }
 .btn_box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  line-height: 50px;
  z-index: 999;
  background: #FAFAFA;
  box-shadow:0px 1px 0px 0px rgba(217,217,217,0.5);
  border-top: 1px solid #D9D9D9;
  text-align: center;
  button {
   font-size: 16px;
   line-height: 50px;
   margin: 0;
   padding: 0;
   border: 1px solid #D9D9D9; //自定義邊框
   outline: none;
  }
  .save_draft{
   color: #B3B3B3;
   border-right: 1px solid #D9D9D9;
  }
  .save_release{
   color: #fff;
   border: 1px solid #00DBD2;
   background: #00DBD2
  }
 }
}
</style>

使用web-view組件傳遞數(shù)據(jù)的問(wèn)題

小程序內(nèi)嵌網(wǎng)頁(yè)向小程序回傳數(shù)據(jù)時(shí),盡量不要使用路由傳參,比如富文本內(nèi)容會(huì)自動(dòng)截取掉src等號(hào)之后的字符串,應(yīng)使用wx.miniProgram.postMessage()方法向小程序發(fā)送數(shù)據(jù)

注意:官方描述--網(wǎng)頁(yè)向小程序 postMessage 時(shí),會(huì)在特定時(shí)機(jī)(小程序后退、組件銷(xiāo)毀、分享)觸發(fā)并收到消息

也就是說(shuō)只有在小程序后退、組件銷(xiāo)毀、分享時(shí)才會(huì)觸發(fā),若無(wú)效可以調(diào)換下順序就可以了
內(nèi)嵌的網(wǎng)頁(yè)代碼:

wx.miniProgram.navigateBack({delta: 1})
wx.miniProgram.postMessage({ data: '數(shù)據(jù)' })

小程序內(nèi)代碼:

<web-view src="{{url}}" bindmessage="handleGetmsg"></web-view>
methods = {
  handleGetmsg (ev) {
   this.data = ev.detail.data[0]
   this.$apply()
  }
}

總結(jié)一下:wepy開(kāi)發(fā)最多的問(wèn)題就是數(shù)據(jù)緩存,組件雙向綁定最好使用twoWay: true來(lái)實(shí)現(xiàn)。

問(wèn)題
原因
解決辦法
描述
子組件接收不到參數(shù)
錯(cuò)誤:<component list="{{list}}">
正確:<component list="list">
修改完布局后不刷新,必須要重新build
將component寫(xiě)到了page文件夾下導(dǎo)致
將component寫(xiě)到components文件夾下
使用flexbox,設(shè)置不換行顯示失效
必須加上新的屬性
加上white-space: nowrap;
很不解,white-space: nowrap;是設(shè)置文字不換行顯示的
異步更新數(shù)據(jù),不刷新
1.沒(méi)有使用this.$apply(); 2.傳入子組件時(shí)需要:prop.sync="data"
1.沒(méi)有使用this.$apply(); 2.傳入子組件時(shí)需要:prop.sync="data"
無(wú)法多次引用同一個(gè)組件
同一個(gè)組件多次引用需要在components中聲明不同的id
不使用組件,完全靠數(shù)據(jù)來(lái)管理狀態(tài)
完全靠數(shù)據(jù)來(lái)驅(qū)動(dòng)的話,不知道對(duì)性能會(huì)不會(huì)有很大影響,待測(cè)試
給data中聲明的屬性賦值,如果該屬性將傳入子組件中,提示內(nèi)存溢出
在子組件中申明的props的屬性名與傳入時(shí)的屬性名不一致
將傳入時(shí)的屬性名和子組件中接收的屬性名保持一致
新建page或component,提示not defined
重命名導(dǎo)致
將dist文件夾刪除,運(yùn)行wepy build,重新生成dist文件夾
微信授權(quán)多個(gè)權(quán)限問(wèn)題
在真機(jī)上請(qǐng)求接口沒(méi)反應(yīng),必須開(kāi)啟調(diào)試模式才行的問(wèn)題
開(kāi)發(fā)時(shí)開(kāi)啟的不校驗(yàn)域名配置,真機(jī)上運(yùn)行除調(diào)試模式外需要域名配置
在微信開(kāi)發(fā)平臺(tái)配置請(qǐng)求域名
上傳圖片只能單個(gè)上傳
不支持多張同時(shí)上傳
循環(huán)上傳
真機(jī)上本地圖片不顯示
寫(xiě)components中的組件引用圖片路徑的問(wèn)題
圖片路徑要寫(xiě)使用這個(gè)組件的page的相對(duì)路徑
使用wxParse后,使用autoprefixer打包報(bào)錯(cuò)
未知
將wxParse.wxss改為wxParse.scss
input多次設(shè)值不改變的問(wèn)題
未知
使用bindinput事件return值重新設(shè)置
后臺(tái)接受中文參數(shù)亂碼
需要轉(zhuǎn)碼
使用encodeURI("參數(shù)")轉(zhuǎn)碼
checkbox設(shè)置大小
使用class設(shè)置transform: scale(0.6);
引用scss樣式文件報(bào)錯(cuò)
<style>標(biāo)簽解析出錯(cuò)
在<style lang="scss">中注明使用類(lèi)型
input設(shè)置值之后不顯示,必須獲取焦點(diǎn)后才會(huì)顯示,失焦后又會(huì)消失
設(shè)置了text-align: 'right'
在input外層包一層view,然后為view設(shè)置固定寬度,注意不能為100%

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

相關(guān)文章

最新評(píng)論