本篇文章給大家總結(jié)下之前開發(fā)微信小程序的時候遇到過一些問題,并將解決方案分享給大家,希望對大家有所幫助!
請以小程序最新文檔為準(zhǔn)~:
https://developers.weixin.qq.com/ebook?action=get_post_info&docid=0008aeea9a8978ab0086a685851c0a&highline=webview
渲染列表時用 block
包裹
<block wx:for="{{[1, 2, 3]}}"> <view> {{index}}: </view> <view> {{item}} </view> </block>
block
不會真實渲染到頁面上,只作為一個包裹元素,接受控制屬性
寫一個自定義組件
自定義組件分為 4 部分
properties 組件接收的屬性
properties: { // 輸入框的默認(rèn)提示 placeholder: { type: String, // 屬性值的類型 value: '' // 屬性默認(rèn)值 } },
data 組件數(shù)據(jù)
methods 組件方法,一般內(nèi)部方法用_開頭
組件的生命周期函數(shù),一般使用 ready,在組件布局完成后執(zhí)行,此時可以獲取節(jié)點信息(使用 SelectorQuery )
調(diào)用父組件傳入的方法
// 子組件 var myEventDetail = {value: ''}; // detail對象,提供給事件監(jiān)聽函數(shù),寫需要傳給外面的數(shù)據(jù) var myEventOption = {} // 觸發(fā)事件的選項 this.triggerEvent('onclear', myEventDetail, myEventOption)
<!-- 父組件 --> <searchbar id="search-bar" bind:onsearch="onSearch" bind:onclear="onSearch" placeholder="搜索文章內(nèi)容"></searchbar> <!-- 像綁定 bindtap 一樣綁定自定義函數(shù) -->
// 父組件 onSearch(e){ console.log(e.detail.value) }
父組件直接調(diào)用子組件的方法
// 父組件,使用 selectComponent 拿到子組件的實例,直接調(diào)用其中的方法 let searchBar = this.selectComponent('#search-bar'); searchBar.setData({ value: e.currentTarget.dataset.name }) searchBar.onClickSearch({ detail: {value: e.currentTarget.dataset.name}});
子組件中獲取 dom 寬高
// 獲取屏幕寬度 let windowWidth = wx.getSystemInfoSync().windowWidth // 在組件內(nèi)部需要寫 this let query = wx.createSelectorQuery().in(this); let that = this; query.selectAll('.tagItem').boundingClientRect() query.exec(function (res) { let allWidth = 0; res[0].map(item=>{ allWidth = allWidth + item.width return allWidth }) let length = res[0].length let ratioWidth = allWidth / windowWidth that.setData({ allLength: length, iphone: ratioWidth + (length == 1 ? 0 : res[0].length * 0.0533) }) })
頁面返回時不會調(diào)用 onLoad
之前把調(diào)用接口的部分寫到了onLoad里,從文章列表進入詳情頁,在從詳情頁點擊左上角回退返回列表頁,列表頁的閱讀數(shù)應(yīng)該更新,但是沒有更新,因為沒有重新調(diào)文章列表接口。
所以把調(diào)文章列表接口的部分寫好了onShow里。
自定義 tabbar 優(yōu)化
第一次優(yōu)化,將組件封裝的tabbar改成頁面的模版形式
1、之前用組件的形式寫的,改為了 template;tabbar 上的圖標(biāo)都改成的 iconfont,解決點擊 tabbar 時候會閃的問題
<template name="tabbar"> <view class="tabbar-wrapper"> <block wx:for="{{tabbar.list}}" wx:key="item"> <navigator hover-class="none" class="tabbar_nav {{item.selected ?'selected':''}}" url="{{item.pagePath}}" style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}" open-type="reLaunch"> <view class="tab-item"><text class="{{item.iconPath}}" style="width: {{item.iconWidth}};height: {{item.iconHeight}}"></text>{{item.text}}<text class='red-tag' wx:if="{{tabbar.num && index==1}}">{{tabbar.num > 99 ? '99+' : tabbar.num}}</text></view> </navigator> </block> </view> </template>
2、點擊 tabbar 時,需要銷毀之前的頁面,在跳到需要跳轉(zhuǎn)的頁面,所以在 navigator 組件用了 reLaunch
第二次優(yōu)化,將帶有tabbar的頁面都封裝成組件寫在頁面,在頁面中setData切換tab
<homePage id="home-page" wx:if="{{tabbarID == tabbarList.home}}" bind:onclicktab="setTabbar" ></homePage> <articleLibraryPage id="article-page" wx:if="{{tabbarID == tabbarList.article}}"></articleLibraryPage> <doclistPage id="doctor-page" wx:if="{{tabbarID == tabbarList.doctor}}"></doclistPage> <mePage id="me-page" wx:if="{{tabbarID == tabbarList.me}}"></mePage> <tabbar id="tab-bar" bind:onclick="onClickTabbar" tabbarID="{{tabbarID}}"></tabbar>
修改的地方:
帶有tabbar的頁面都重寫為組件形式
因為組件中只有掛載完成后的 ready 方法,所以之前頁面中 onShow,onReachBottom,onPullDownRefresh 都放到父頁面調(diào)用
onPullDownRefresh: function () { if (this.data.tabbarID === this.data.tabbarList.article) { // 使用 selectComponent 找到組件實例,調(diào)用內(nèi)部方法 let articlePage = this.selectComponent('#article-page'); articlePage.onPullDownRefresh(); } else if (this.data.tabbarID === this.data.tabbarList.doctor){ let doctorPage = this.selectComponent('#doctor-page'); doctorPage.onPullDownRefresh(); } else { wx.stopPullDownRefresh(); } },
帶來的問題:
每個tabbar都會有下拉刷新的效果,即使不需要下拉刷新
從其他頁面點擊按鈕,直接跳到首頁的某一個tab卡,可能會有問題
使用 iconfont
https://www.jianshu.com/p/1cfc074eeb75
登錄 iconfont.cn 下載 zip 包
解壓縮,其中的 .ttf 文件在 transfonter.org/ 上面轉(zhuǎn)成 base64 格式
將 style.css 寫入新建的 iconfont.wxss 中,上面的字體文件路徑用剛剛轉(zhuǎn)好的 base64 替代
在 app.wxss 中 import iconfont.wxss
注意:組件中的樣式本身不受 app.wxss 影響,因此,組件中需要使用 iconfont 的時候,需要手動引一下 app.wxss 或者 iconfont.wxss
列表的左滑效果
1、在列表的父元素上綁定事件
<view class="list-container" wx:for="{{doctorList.list}}" wx:key="{{index}}" > <view bindtouchstart='onTouchStartListItem' bindtouchmove='onTouchMoveListItem' style="{{item.txtStyle}}" >滑動的內(nèi)容 </view> <view class="backCover">滑動后顯示的按鈕</view> </view>
.list-container{ position: relative; width:100%; height: 224rpx; overflow: hidden; } .list-item{ position: absolute; left: 0; z-index: 5; transition: left 0.2s ease-in-out; background-color: #fff; } .backCover{ box-sizing: border-box; width: 200rpx; height: 218rpx; position: absolute; right: 0; top: 0; z-index: 4; }
2、通過判斷滑動距離修改列表項的 left 值
onTouchStartListItem: function (e) { // 是單指觸碰 if (e.touches.length === 1) { // 記下觸碰點距屏幕邊緣的x軸位置 this.setData({ startX: e.touches[0].clientX, }) } }, onTouchMoveListItem: function (e) { var that = this if (e.touches.length == 1) { var disX = that.data.startX - e.touches[0].clientX; var deleteBtnWidth = that.data.deleteBtnWidth; var txtStyle = ""; if (disX < deleteBtnWidth / 4) { txtStyle = "left:0rpx"; } else { txtStyle = "left:-" + deleteBtnWidth + "rpx"; } var index = e.currentTarget.id var list = that.data.doctorList.list list[index].txtStyle = txtStyle; that.setData({ doctorList: { list: list, total: that.data.doctorList.total } }) } }, onTouchEndListItem: function (e) { var that = this if (e.changedTouches.length == 1) { var endX = e.changedTouches[0].clientX; var disX = that.data.startX - endX; var deleteBtnWidth = that.data.deleteBtnWidth; var txtStyle = disX > deleteBtnWidth / 2 ? "left:-" + deleteBtnWidth + "px" : "left:0px"; var index = e.currentTarget.id var list = that.data.doctorList.list list[index].txtStyle = txtStyle; that.setData({ doctorList: { list: list, total: that.data.doctorList.total } }); } },
【相關(guān)學(xué)習(xí)推薦:小程序開發(fā)教程】
以上是總結(jié)分享一些小程序開發(fā)中遇到的問題(幫忙避坑)的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

抖音網(wǎng)頁版的登錄入口是https://www.douyin.com/。登錄步驟包括:1.打開瀏覽器;2.輸入網(wǎng)址https://www.douyin.com/;3.點擊“登錄”按鈕并選擇登錄方式;4.輸入賬號密碼;5.完成登錄。網(wǎng)頁版提供了瀏覽、搜索、互動、上傳視頻和個人主頁管理等功能,具有大屏幕體驗、多任務(wù)處理、便捷的賬號管理和數(shù)據(jù)統(tǒng)計等優(yōu)勢。

拷貝漫畫無疑是一個不容錯過的寶藏。在這里,你可以找到各種風(fēng)格的籃球漫畫,從熱血勵志的競技故事,到輕松幽默的日常喜劇,應(yīng)有盡有。無論是想重溫經(jīng)典,還是想發(fā)掘新作,拷貝漫畫都能滿足你的需求。通過拷貝漫畫提供的正版在線閱讀入口,你將告別盜版資源的困擾,享受高清流暢的閱讀體驗,更能支持你喜愛的漫畫作者,為正版漫畫的發(fā)展貢獻一份力量。

選擇UC瀏覽器還是QQ瀏覽器取決于你的需求:1.UC瀏覽器適合追求快速加載和豐富娛樂功能的用戶;2.QQ瀏覽器適合需要穩(wěn)定性和與騰訊產(chǎn)品無縫連接的用戶。

結(jié)合 2025 年最新行業(yè)動態(tài)與多維度評測數(shù)據(jù),以下為綜合排名前十的 AI 寫作軟件推薦,涵蓋通用創(chuàng)作、學(xué)術(shù)研究、商業(yè)營銷等主流場景,同時兼顧中文優(yōu)化與本地化服務(wù):

奈斯漫畫,一個致力于為漫畫愛好者打造的沉浸式閱讀體驗平臺,匯聚了海量國內(nèi)外優(yōu)質(zhì)漫畫資源。它不僅僅是一個漫畫閱讀平臺,更是一個連接漫畫家與讀者、分享漫畫文化的社區(qū)。通過簡潔直觀的界面設(shè)計和強大的搜索功能,奈斯漫畫讓你能夠輕松找到心儀的作品,享受流暢舒適的閱讀體驗。告別漫長的等待和繁瑣的操作,即刻進入奈斯漫畫的世界,開啟你的漫畫之旅吧!

蛙漫漫畫,憑借其豐富多元的漫畫資源和便捷流暢的在線閱讀體驗,已成為眾多漫畫愛好者的首選。它就像一個充滿活力的池塘,源源不斷地涌現(xiàn)出新鮮有趣的故事,等待著你去發(fā)現(xiàn)和探索。蛙漫漫畫涵蓋了各種題材,從熱血冒險到甜蜜戀愛,從奇幻科幻到懸疑推理,無論你喜歡哪種類型,都能在這里找到心儀的作品。其簡潔直觀的界面設(shè)計,更讓你能夠輕松上手,快速找到想看的漫畫,沉浸在精彩紛呈的漫畫世界中。

在這里,您可以盡情暢游于浩瀚的漫畫海洋,探索各種題材和風(fēng)格的作品,從熱血激昂的少年漫,到細(xì)膩動人的少女漫,從懸疑燒腦的推理漫,到輕松搞笑的日常漫,應(yīng)有盡有,總有一款能夠觸動您的心弦。我們不僅擁有海量的正版漫畫資源,還不斷引進和更新最新的作品,確保您能夠第一時間閱讀到您喜愛的漫畫。

2025b安最新官網(wǎng)入口地址:https://www.marketwebb.co/zh-CN/join?ref=507720986&type=wenzi;幣安(Binance)交易所是一家全球性的加密貨幣交易所,服務(wù)包括北美、歐洲、臺灣、中東、香港、馬來西亞在內(nèi)的180個國家地區(qū),提供超過600種加密貨幣,在全球擁有2.7億注冊用戶。
