微信小程序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.html

<block wx:for="{{studentList}}" wx:for-item="item" wx:for-index="idx">
        <view>
          {{item.name}}
        </view>
</block>

因为取的是name,所以最后输出的是xiaoming    和xiaohong。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM