网络编程
位置:首页>> 网络编程>> JavaScript>> 微信小程序module.exports模块化操作实例浅析

微信小程序module.exports模块化操作实例浅析

作者:CoKeny  发布时间:2023-07-02 05:30:27 

标签:微信小程序,module.exports,模块化

本文实例讲述了微信小程序module.exports模块化操作。分享给大家供大家参考,具体如下:

微信小程序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。如下图所示(这里就懒得弄样式了~):

微信小程序module.exports模块化操作实例浅析

希望本文所述对大家微信小程序开发有所帮助。

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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com