1. 給對象的屬性賦值
因為小程序不能直接給對象屬性賦值孵睬,要通過特殊處理 ['value']
data:
fromData: {
inputValue: '',
}
handleTextArea(e) {
let inputValue = 'formData.inputValue'
this.setData({
[inputValue]: e.detail.value
})
},
2. 微信小程序在wxml雙大括號中執(zhí)行復雜運算
微信小程序數(shù)據(jù)綁定支持雙大括號中的表達式晶伦,但目前只支持這四種表達式:算數(shù)表達式崩泡、關系表達式、字符串連接表達式耐版、三元表達式俐巴。vue則是全功能的JS表達式。使用起來明顯差別在于是否支持函數(shù)/方法表達式
1. 遇到的問題
<!-- 有效 -->
<view>{{ 1+1 }}</view>
<view>{{ 2>1 ? '是':'否'}}</view>
<!-- 無效 -->
<view>{{ Math.random() > 0.5 ? '大于':'小于' }}</view>
<view wx:for="{{'1,2,3'.split(',')}}">
{{item}}
</view>
2.解決方法
<view wx:for="{{fn.split(str)}}">
{{item}}
</view>
<wxs module="fn">
module.exports = {
split: function(str){
return str.split(',');
}
}
</wxs>
3.實例
<view class="imgList">
<wxs module="fn">
module.exports = {
split: function (str) {
return str.split(',');
}
}
</wxs>
<block wx:for="{{fn.split(item.COMIMG)}}">
<image src="{{imgAddr}}/{{item}}"></image>
</block>
</view>
3. 小程序tabbar跳轉用到的 wx.switchTab 沒法攜帶參數(shù)
跳轉到 tabBar 頁面牵囤,并關閉其他所有非 tabBar 頁面
可通過 app.js 添加全局屬性
app.js
globalData: {
currentIndex: 0
}
index.js
//通過調用全局屬性更改數(shù)據(jù)
//訂單跳轉
toOrderListTap(e) {
getApp().globalData.currentIndex= e.currentTarget.dataset.index;
wx.switchTab({
url: '/pages/order/order'
})
},