微信小程序 開發(fā)中遇到問題總結
微信小程序 開發(fā)中遇到問題總結
1.由于小程序wx.request()方法是異步的,在app.js執(zhí)行ajax后,各分頁加載app.js的全局數(shù)據(jù)時,無法按順序加載。例:
//app.js App({ ajax:function(){ let that = this; wx.request({ url: 'https://a.com/url.php', method: 'GET', success: function(e){ that.data = 123; } }) }; }) //content.js let app = getApp() Page({ getData: function(){; app.ajax(); console.log(app.data); //undefined } })
解決方法,使用Promise異步函數(shù):
//app.js App({ ajax:function(){ let that = this; let promise = new Promise(function(resolve, reject){ wx.request({ url: 'https://a.com/url.php', method: 'GET', success: function(e){ that.data = 123; resolve(); } }) }); }; }) //content.js let app = getApp() Page({ getData: function(){; app.ajax().then(()=>{ console.log(app.data); //123 }); } })
2.圖片只能獲取原始寬高,無法獲取現(xiàn)有寬高。不過image標簽封裝了mode屬性,可以根據(jù)需求自行設置。
3.每個image標簽底部有一條透明間隔,非padding,非margin。在圖片前面做遮罩層時可能會被坑。
4.網(wǎng)絡請求必須部署https
5.配置tabBar時,list參數(shù)中的pagePath參數(shù)至少需要包含app.json里pages數(shù)組中的第一個路徑,否則會導致tabBar不顯示。
6.tabBar跳轉時無法帶參數(shù),解決方法:
//search.js var app = getApp(); Page({ confirm: function(e){ //獲取數(shù)據(jù),添加到全局 let val = e.detail.value; app.searchWord = val; this.jump(); }, jump: function(){ //跳轉tabBar wx.switchTab({ url: '../index/index', }); }, }); //index.js var app = getApp(); Page({ onShow: function(e){ //獲取全局數(shù)據(jù) let val = app.searchWord; } }); //需要傳遞參數(shù)的頁面在跳轉前將數(shù)據(jù)添加到app.js里。需要接受參數(shù)的頁面在onShow方法接受之前添加到app.js的數(shù)據(jù)。
7.小程序wx.request()方法請求的url必須是https開頭
8.wx.request()使用post方法請求時,還需要加上header,header[content-type]值為application/x-www-form-urlencoded。例:
wx.request({ url: 'https://a.com/url.php', data: {message: 123}, method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function(e){ console.log(e) } });
9.小程序無法加載html標簽,同時數(shù)據(jù)渲染也無法渲染wxml標簽(<view></view>等),可以使用wxParse.js來處理相關數(shù)據(jù)。
10.安卓無法渲染wx.request()請求的數(shù)據(jù)。
檢測返回的數(shù)據(jù)是否有BOM頭(3個字符的空白)。安卓的wx.request解析不會跳過BOM頭,導致數(shù)據(jù)返回的是字符串,而不是對象或者數(shù)組。
例:
返回的數(shù)據(jù)是:(3個字符的空白){a:1, b:2}
解析的數(shù)據(jù)是:'{a:1, b:2}'(字符串),而不是{a:1, b:2}(對象)
由于不是對象,模板渲染之類會無法正常進行。解決方法,后臺返回數(shù)據(jù)前去掉BOM頭就行。如果后臺不會去BOM頭,可以在前端去除,但是wx.request如果dataType缺省,會默認為json并自動解析,導致無法去除BOM頭。
解決方案:
wx.request({ url: url, method: 'GET', dataType: 'txt', success: function(e){ let json = e.data.trim(); let arr = JSON.parse(json); } });
dataType改為json以外的格式,避免小程序自動解析json字符串,然后對返回的數(shù)據(jù)用 trim() 方法去掉空白,最后解析json字符串就行。
11.調(diào)試時多行省略(-webkit-line-clamp)正常,發(fā)布時多行省略無效。
解決方案:如果不想重新審核,讓后臺截斷就好
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Java解決No enclosing instance of type PrintListFromTailToHead
這篇文章主要介紹了Java解決No enclosing instance of type PrintListFromTailToHead is accessible問題的兩種方案的相關資料,需要的朋友可以參考下2016-07-07SpringBoot集成quartz實現(xiàn)定時任務
這篇文章主要介紹了如何使用SpringBoot整合Quartz,并將定時任務寫入庫中(持久化存儲),還可以任意對定時任務進行如刪除、暫停、恢復等操作,需要的可以了解下2023-09-09intellij idea中spring boot properties文件不能自動提示問題解決
這篇文章主要介紹了intellij idea中spring boot properties文件不能自動提示問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02