詳解wepy開(kāi)發(fā)小程序踩過(guò)的坑(小結(jié))
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)文章
js下為表格內(nèi)部動(dòng)態(tài)添加行的代碼
最近的一個(gè)項(xiàng)目中在保存表單數(shù)據(jù)時(shí),要用到一個(gè)動(dòng)態(tài)添加行的功能。平時(shí)動(dòng)態(tài)添加行只是在表格的最下面添加,現(xiàn)在在表格中間動(dòng)態(tài)添加行,而且表格內(nèi)部是包含并且單元格的,其實(shí)很簡(jiǎn)單,下面貼出代碼。2010-06-06基于JavaScript實(shí)現(xiàn)活動(dòng)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)活動(dòng)倒計(jì)時(shí)效果,距離活動(dòng)時(shí)間還剩多少,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04js驗(yàn)證真實(shí)姓名與身份證號(hào),手機(jī)號(hào)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇js驗(yàn)證真實(shí)姓名與身份證號(hào),手機(jī)號(hào)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07javascript數(shù)據(jù)結(jié)構(gòu)之串的概念與用法分析
這篇文章主要介紹了javascript數(shù)據(jù)結(jié)構(gòu)之串的概念與用法,簡(jiǎn)單講述了串的概念、功能并結(jié)合實(shí)例形式分析了基于javascript實(shí)現(xiàn)串的遍歷、比較、查找等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲
本文主要介紹了js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲的實(shí)例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03layui 根據(jù)后臺(tái)數(shù)據(jù)動(dòng)態(tài)創(chuàng)建下拉框并同時(shí)默認(rèn)選中的實(shí)例
今天小編就為大家分享一篇layui 根據(jù)后臺(tái)數(shù)據(jù)動(dòng)態(tài)創(chuàng)建下拉框并同時(shí)默認(rèn)選中的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09javaScript強(qiáng)制保留兩位小數(shù)的輸入數(shù)校驗(yàn)和小數(shù)保留問(wèn)題
這篇文章主要介紹了javaScript強(qiáng)制保留兩位小數(shù)的輸入數(shù)校驗(yàn)和小數(shù)保留問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05微信小程序開(kāi)發(fā)教程-手勢(shì)解鎖實(shí)例
手勢(shì)解鎖是app上常見(jiàn)的解鎖方式,相比輸入密碼方式操作起來(lái)要方便許多。這篇文章主要介紹了微信小程序開(kāi)發(fā)教程-手勢(shì)解鎖實(shí)例,有興趣的可以了解一下。2017-01-01