核心:使用wx:for="{{posts_key}}",用数组的形式来绑定多个的内容,利用for循环来反复调用后台数据,如果没用用posts_key了,那么对应的item_这种命名就是不可以的!
Wxml
<block wx:for="{{posts_key}}" wx:for-item="item" wx:for-index="idx" wx:key="idx"> <view catchtap="onPostTap" data-postId="{{item.postId}}"> <image src="{{item.images}}"></image> <text>{{item.content}}</text> </view> </block>
JS
var postsData = require(‘../../data/skip-data.js‘) const app = getApp() Page({ data: { }, onLoad:function(options){ this.setData({ posts_key:postsData.postList }); }, })
Skip-data,js(这个脚本用来模拟本地数据库)
var local_database = [ { content: "VIPKID 纯北美外教 代言人刘涛", images: "/images/dian1@2x.png", postId: 0, }, { content: "哒哒英语 专属外教 代言人孙俪", images: "/images/dian1@2x.png", postId: 1, }, { content: "51Talk 高校教材 代言人贾乃亮", images: "/images/dian1@2x.png", postId: 2, }, { content: "VipJr 量身定制课 代言人姚明", images: "/images/dian1@2x.png", postId: 3, }, { content: "以上都想试听", images: "/images/dian1@2x.png", postId: 4, }, ] module.exports = { postList: local_database }