Python+微信小程序开发(五)for指令和上传图片

本篇将分享如何使用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+微信小程序开发』系列,持续更新中。

需要详细代码和交流的小伙伴们可以关注下方链接。

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yunlord

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值