本文實例講述了微信小程序module.exports模塊化操作。分享給大家供大家參考,具體如下:
文件 目錄如上圖:
看到網上寫的模塊化都比較復雜,寫個入門版的 好讓大家理解理解
common.js
var studentList = [ { name: "xiaoming", age: "22", hobby: "sleep" }, { name: "xiaohong", age: "22", hobby: { one: "eat", two: "eatfood" } }]//模塊化module.exports = { studentList: studentList}
index.js:
var common = require("../aa/common.js")//獲取應用實例var app = getApp()Page({ data: { }, onLoad: function () { this.setData({ studentList:common.studentList }); }})
index.wxml:
<block wx:for="{{studentList}}" wx:for-item="item" wx:for-index="idx"> <view> {{item.name}} </view></block>
因為取的是name,所以最后輸出的是xiaoming 和xiaohong。如下圖所示(這里就懶得弄樣式了~):
希望本文所述對大家微信小程序開發有所幫助。
新聞熱點
疑難解答