微信小程序js模块化

模块化:把一些公共js函数抽离出来使用

使用model.exports来暴露模块接口

// common.js
function sayHello(name) {
  console.log(`Hello ${name} !`)
}
function sayGoodbye(name) {
  console.log(`Goodbye ${name} !`)
}

module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
如果在其他文件中引用:使用require
  var common = require('common.js')
Page({
  helloMINA: function() {
    common.sayHello('MINA')
  },
  goodbyeMINA: function() {
    common.sayGoodbye('MINA')
  }
})
  

 

posted @ 2021-10-02 01:44  花生又叫二花深  阅读(77)  评论(0)    收藏  举报