微信小程序 模块化


本文整理自网络,侵删。

文件作用域

在JavaScript文件中声明的变量和函数只在该文件中有效;不同的文件中可以声明相同名字的变量和函数,不会互相影响。

通过全局函数getApp()可以获取全局的应用实例,如果需要全局的数据可以在App()中设置,如:

// app.js
App({
  globalData: 1
})
// a.js
// The localValue can only be used in file a.js.
var localValue = 'a'
// Get the app instance.
var app = getApp()
// Get the global data and change it.
app.globalData++
// b.js
// You can redefine localValue in file b.js, without interference with the localValue in a.js.
var localValue = 'b'
// If a.js it run before b.js, now the globalData shoule be 2.
console.log(getApp().globalData)

模块化

我们可以将一些公共的代码抽离成为一个单独的js文件,作为一个模块。模块只有通过module.exports或者 exports才能对外暴露接口。

阅读剩余部分

相关阅读 >>

微信小程序 q&a

微信小程序 小程序使用realmockupdateorder

微信小程序api nfc-isodep标签

微信小程序api-设备-加速度计

微信小程序云开发sdk文档 微信支付申请退款

微信小程序api 地图-创建mapcontext对象

微信小程序api 创建插屏广告组件

微信小程序承载网页 web-view

微信小程序 性能trace工具

微信小程序 快递接口(商家查看)-相关接口和时间

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...