首頁  >  文章  >  微信小程式  >  總結分享一些小程式開發中遇到的問題(幫忙避坑)

總結分享一些小程式開發中遇到的問題(幫忙避坑)

青灯夜游
青灯夜游轉載
2022-02-08 10:07:433093瀏覽

這篇文章給大家總結下之前開發微信小程式的時候遇到過一些問題,並將解決方案分享給大家,希望對大家有幫助!

總結分享一些小程式開發中遇到的問題(幫忙避坑)

請以小程式最新文件為準~:

https://developers.weixin.qq.com/ebook?action= get_post_info&docid=0008aeea9a8978ab0086a685851c0a&highline=webview

##block 不會真實渲染到頁面上,只作為一個包裹元素,接受控制屬性#寫一個自訂元件

#自訂元件分為4 部分

properties 元件接收的屬性

<block wx:for="{{[1, 2, 3]}}">
  <view> {{index}}: </view>
  <view> {{item}} </view>
</block>

    data 元件資料
  • methods 元件方法,一般內部方法用_開頭
// 父组件
onSearch(e){
  console.log(e.detail.value)
}

子元件中取得dom 寬高

// 父组件,使用 selectComponent 拿到子组件的实例,直接调用其中的方法
let searchBar = this.selectComponent(&#39;#search-bar&#39;);
searchBar.setData({ value: e.currentTarget.dataset.name })
searchBar.onClickSearch({ detail: {value: e.currentTarget.dataset.name}});

#頁面返回時不會調用onLoad

之前把調用接口的部分寫到了onLoad裡,從文章列表進入詳情頁,在從詳情頁點擊左上角回退返回列表頁,列表頁的閱讀數應該更新,但是沒有更新,因為沒有重新調文章列表介面。

所以把調文章列表介面的部分寫好了onShow裡。 自訂tabbar 最佳化

第一次最佳化,將元件封裝的tabbar改為頁面的模版形式

1.之前用元件的形式寫的,改為了template;tabbar 上的圖示都改成的iconfont,解決點擊tabbar 時候會閃的問題

// 获取屏幕宽度
let windowWidth = wx.getSystemInfoSync().windowWidth
// 在组件内部需要写 this
let query = wx.createSelectorQuery().in(this);
let that = this;
query.selectAll(&#39;.tagItem&#39;).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)
	})
})
2、點擊tabbar 時,需要銷毀之前的頁面,在跳到需要跳轉的頁面,所以在navigator 組件用了reLaunch

#第二次優化,將帶有tabbar的頁面都封裝成組件寫在頁面,在頁面中setData切換tab

<template name="tabbar">
	<view class="tabbar-wrapper">
		<block wx:for="{{tabbar.list}}" wx:key="item">
			<navigator hover-class="none" class="tabbar_nav {{item.selected ?&#39;selected&#39;:&#39;&#39;}}"  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=&#39;red-tag&#39; wx:if="{{tabbar.num && index==1}}">{{tabbar.num > 99 ? &#39;99+&#39; : tabbar.num}}</text></view>
			</navigator>
		</block>
	</view>
</template>

修改的地方:

有tabbar的頁面都重寫為元件形式

因為元件中只有掛載完成後的ready 方法,所以之前頁面中onShow,onReachBottom,onPullDownRefresh 都會放到父頁呼叫

  • <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都會有下拉刷新的效果,即使不需要下拉刷新

#從其他頁面點擊按鈕,直接跳到首頁的某一個tab卡,可能會有問題

  • 使用iconfont
  • #https://www.jianshu.com/p/1cfc074eeb75

登入iconfont.cn 下載zip 套件

#解壓縮,其中的.ttf 檔案在

transfonter.org/
    上面轉成base64 格式
  • 將style.css 寫入新建的iconfont.wxss 中,上面的字體檔案路徑用剛剛轉好的base64 取代
  • #在app.wxss 中import iconfont.wxss

  • #注意:元件中的樣式本身不受app.wxss 影響,因此,元件中需要使用iconfont 的時候,需要手動引一下app.wxss 或iconfont.wxss

  • 清單的左滑效果

1、在清單的父元素上綁定事件

onPullDownRefresh: function () {
	if (this.data.tabbarID === this.data.tabbarList.article) {
	  // 使用 selectComponent 找到组件实例,调用内部方法
	  let articlePage = this.selectComponent(&#39;#article-page&#39;);
	  articlePage.onPullDownRefresh();
	} else if (this.data.tabbarID === this.data.tabbarList.doctor){
	  let doctorPage = this.selectComponent(&#39;#doctor-page&#39;);
	  doctorPage.onPullDownRefresh();
	} else {
	  wx.stopPullDownRefresh();
	}
},
<view  
	class="list-container"
	wx:for="{{doctorList.list}}"
	wx:key="{{index}}"
>
	<view
		bindtouchstart=&#39;onTouchStartListItem&#39;
		bindtouchmove=&#39;onTouchMoveListItem&#39;
		style="{{item.txtStyle}}"
	>滑动的内容
	</view>
	<view class="backCover">滑动后显示的按钮</view>
</view>

2、透過判斷滑動距離修改清單項目的left 值

.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;
}
#【相關學習推薦:小程式開發教學

以上是總結分享一些小程式開發中遇到的問題(幫忙避坑)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.cn。如有侵權,請聯絡admin@php.cn刪除