美文网首页小程序前端开发
小程序调用图片接口API并居中显示

小程序调用图片接口API并居中显示

作者: 祈澈菇凉 | 来源:发表于2019-06-25 01:59 被阅读145次

写完调用天气接口的demo之后,小程序调用天气接口并且渲染在页面
https://www.jianshu.com/p/5bbe8fb72c7c,顺便再调用了一下美图的接口API:

美图API:https://www.apiopen.top/api.html#feb14cd9ff0a4e4e9eda897858f30f21
url:https://api.apiopen.top/getImages

wxml:

    <view class='imagesize' wx:for="{{list}}" wx:key="index">
    <image src="{{list.img}}" class='in-image' >
    </image>
    </view>

js:

Page({
  data: {
    list: []
  },

  onLoad: function (options) {

    wx.request({
      url: 'https://api.apiopen.top/getImages',

      header: {
        'content-type': 'application/json'
      },
      success: res => {
        console.log(res.data)
        this.setData({
          //第一个data为固定用法,第二个data是json中的data
          list: res.data.result[0],
        })
      }
    })
  },

})

css:

.imagesize {
  display: flex;
  justify-content: center;
}

.imagesize image {
  width: 400rpx;
  height: 400rpx;
}

注意
以上代码里面每次刷新的时候,都会随机调用接口数据
因为接口文档里面有说明,传0或者不传会随机推荐图片


所以我们在掉接口的时候可以在url后面绑定参数:

url: 'https://api.apiopen.top/getImages?page=1&count=2',

原文作者:祈澈姑娘 技术博客:https://www.jianshu.com/u/05f416aefbe1
90后前端妹子,爱编程,爱运营,文艺与代码齐飞,魅力与智慧共存的程序媛一枚。
欢迎关注【编程微刊】公众号,回复【领取资源】,500G编程学习资源干货免费送。

相关文章

网友评论

    本文标题:小程序调用图片接口API并居中显示

    本文链接:https://www.haomeiwen.com/subject/oivyqctx.html