本篇將分享如何使用for指令以及獲得上傳圖片,這感覺在大部分小程式都比較實用!!!
一、for指令
首先還是新建pages/goods/goods的檔案夾,
1.回圈串列
在pages/goods/goods.js填寫dataList:
data: {
dataList:['手機','手表','耳機']
},
在pages/goods/goods.wxml中,我們用到wx:for陳述句,
<view>
<view wx:for="{{dataList}}">{{item}}</view>
</view>

還可以添加索引:
<view wx:for="{{dataList}}">{{index}}-{{item}}</view>

還可以自定義索引名和item名字:
<view wx:for="{{dataList}}" wx:for-index='idx' wx:for-item='x'>自定義{{idx}}-{{x}}</view>

2.回圈字典
在pages/goods/goods.js填寫userInfo:
data: {
dataList:['手機','手表','耳機'],
userInfo:{
name:'Yunlord',
age:24
}
},
在pages/goods/goods.wxml中,填寫以下代碼:
<view>
{{userInfo.name}}
{{userInfo.age}}
</view>
<view>
<view wx:for="{{userInfo}}">{{index}} - {{item}}</view>
</view>
效果如下:

二、獲取用戶上傳圖片
新建pages/publish/publish檔案夾,
在pages/publish/publish.wxml填下以下代碼:
<text bindtap="uploadImage">請上傳圖片</text>
<view class="container">
<image wx:for="{{imageList}}" src="{{item}}"></image>
</view>
在pages/publish/publish.js填寫imageList和uploadImage函式:
data: {
imageList:['/static/profile.png','/static/profile.png']
},
uploadImage:function(){
var that=this
wx.chooseImage({
count: 9,
sizeType:['original'],
sourceType:['album','camera'],
success:function(res){
that.setData({imageList:res.tempFilePaths})
}
})
},
效果如下:

添加新的圖片,而不是覆寫,
that.setData({
imageList: that.data.imageList.concat(res.tempFilePaths)
})

今天的知識點就學習完畢了!!!
歡迎關注『從零開始Python+微信小程式開發』系列,持續更新中,
需要詳細代碼和交流的小伙伴們可以關注下方鏈接,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337706.html
標籤:python
