微信小程序module.exports 模块化

微信小程序module.exports 模块化

     文件 目录如上图:

    看到网上写的模块化都比较复杂,写个入门版的  好让大家理解理解

//common.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

var studentList = [

    {

        name: "xiaoming",

        age: "22",

        hobby: "sleep"

    },

    {

        name: "xiaohong",

        age: "22",

        hobby: {

            one: "eat",

            two: "eatfood"

        }

    }

]

//模块化

module.exports = {

    studentList: studentList

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//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。

https://www.cnblogs.com/panlaixing/p/6736499.html

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/81515107