原創(chuàng)文章,歡迎轉(zhuǎn)載秦忿。轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自IT人故事會(huì)默赂,謝謝沛鸵!
原文鏈接地址:「小程序JAVA實(shí)戰(zhàn)」 小程序抽離公用方法進(jìn)行模塊化(12)
小程序的模塊化,把磚磊成一個(gè)墩子,用的時(shí)候把整個(gè)墩子移走曲掰。js更好的調(diào)用疾捍,應(yīng)用更加公用化。源碼:https://github.com/limingios/wxProgram.git 中的No.7
小程序的模塊化
抽離通用方法作為通用函數(shù)
構(gòu)建utils-common類
- 官方的闡述
https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html
- 程序演示
events.js
//events.js
//獲取應(yīng)用實(shí)例
const app = getApp()
var common = require('../untils/common.js')
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
clickMe: function(e){
console.log("你點(diǎn)擊我這里出來了!")
console.log(e)
console.log(e.currentTarget.dataset.fordate)
common.sayHello("公眾號(hào):編程坑太多")
common.sayGoodbye("[編程坑太多]")
}
})
common.js
// common.js
function sayHello(name) {
console.log(`Hello ${name} !`)
console.log("Hello "+name+" !")
}
function sayGoodbye(name) {
console.log(`Goodbye ${name} !`)
console.log("Goodbye " + name + " !")
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
PS:需要注意的是
console.log(`Goodbye ${name} !`)
console.log("Goodbye " + name + " !")
區(qū)別如果用了 ${} 最外層需要用``符號(hào)栏妖,如果你喜歡老套路可以按照我的 "Goodbye " + name + " !" 這種乱豆。