1、question-data.js
var local_question = [...
]
module.exports = { //數據暴露出去
questions: local_question
}
2、數據綁定post.js
var questionData=require('../../data/question-data.js'); //引入
data: { //小程序總是會讀取data對來做數據綁定,這個動作我們稱爲
//動作a,而這個動作A是在load事件執行之後發生的
},
/**
* 生命週期函數--監聽頁面加載 頁面初始化 on開頭的函數監聽函數
*/
onLoad: function (options) {
this.setData({questions:questionData.questions}); //如果是異步操作賦值那麼必須用this.setData
},
3、顯示post.wxml
<block wx:for="{{questions}}" wx:for-item="item">
<view class='post-container'>
<view class='post-author-date'>
<image src='{{item.avatar}}'></image>
<text>{{item.name}}</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>