小程序---引用本地js的json数据

1、posts-data.js

var local_database = [...

]

module.exports = { //数据暴露出去
postList: local_database
}

2、数据绑定post.js

var postsData= require( '../../data/posts-data.js'); //引入

data: { //小程序总是会读取data对来做数据绑定,这个动作我们称为
//动作a,而这个动作A是在load事件执行之后发生的
},
/**
* 生命周期函数--监听页面加载 页面初始化 on开头的函数监听函数
*/
onLoad: function (options) {
// this.data.postList =postsData.postList; //数据绑定的两种方法 不建议使用
this.setData({posts_key:postsData.postList}); //如果是异步操作赋值那么必须用this.setData
},

3、显示post.wxml

< block wx:for= "{{posts_key}}" wx:for-item= "item">
< view class= 'post-container'>
< view class= 'post-author-date'>
< image src= '{{item.avatar}}'></ image >
< text >{{item.author}} </ text >
</ view >
< text class= 'post-title'>{{item.title}} </ text >
< image class= 'post-image' src= '{{item.imgSrc}}'></ image >
< text class= 'post-content'>{{item.content}} </ text >
< view class= 'post-like'>
< image class= 'view' src= '/images/view.png'></ image >
< text class= 'view-txt'>92 </ text >
< image class= 'collect' src= '/images/star.jpg'></ image >
< text class= 'collect-txt'>65 </ text >
</ view >
</ view >
</ block >


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值