如何wx:for遍历一个文件夹中所有图片?
发布于 6 年前 作者 na13 11270 次浏览 来自 官方Issues

想做一个swiper轮播图,在swiper-item前加一个block块用于循环,现有两个问题:

一、有没有更好的思路?

二、block块中如何使用wx:for遍历一个文件夹中所有图片?

我是一个快40岁的小白,请大神勿喷,如有回复请写名具体代码,谢谢啦

6 回复

wxml部分

<swiper indicator-dots="true"
  autoplay="true" interval="5000" duration="1000" circular="true" previous-margin="50" next-margin="50">
  <block wx:for="{{imgUrls}}">
    <swiper-item>
      <image src="{{item.images}}" data-url="{{item.url}}" bindtap="onjump" class="slide-image" width="355" height="150"/>
    </swiper-item>
  </block>

</swiper>

js部分

data: {
    imgUrls: [
      {
        images: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        url:'/pages/index/index'
      },
       {
         images: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
        url: '/pages/index/index1'
      },
       {
         images: 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
        url: '/pages/index/index2'
      }
    ],
    
  },

今天刚好给别人写了一个,要熟悉下小程序中for循环的运用

谢谢大家了

小程序没发自己去读文件夹,只能你把路径写成数组变量哦

只能自己手动写一个数组把你文件夹的图面相对路径写入,然后页面遍历数组

如果是读取用户手机中的文件,可以用相关接口拿到文件路径后放到数组中,wxml中for循环。如果文件存在服务器上,需要后端接口把文件链接给到小程序,wxml中for循环

前端是遍历不了文件夹的文件的,这个需要服务端的支持。

回到顶部