微信小程序如何调用图片接口API并居中显示

Bella ·
更新时间:2024-09-20
· 911 次阅读

写完调用天气接口的demo之后,小程序调用天气接口并且渲染在页面,顺便再调用了一下美图的接口API:

美图API

url

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',



小程序 调用 程序 居中 图片 微信小程序 api

需要 登录 后方可回复, 如果你还没有账号请 注册新账号