「小程序JAVA实战」 小程序抽离公用方法进行模块化(12)
小程序的模块化,把砖磊成一个墩子,用的时候把整个墩子移走。js更好的调用,应用更加公用化。源码:https://github.com/limingios/wxProgram.git 中的No.7
小程序的模块化
抽离通用方法作为通用函数
构建utils-common类
官方的阐述
>https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html
程序演示
events.js
//events.js
//获取应用实例
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("你点击我这里出来了!")
console.log(e)
console.log(e.currentTarget.dataset.fordate)
common.sayHello("公众号:编程坑太多")
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 = sayGoodbyePS:需要注意的是
console.log(`Goodbye ${name} !`)
console.log("Goodbye " + name + " !")区别如果用了 ${} 最外层需要用“符号,如果你喜欢老套路可以按照我的 “Goodbye ” + name + ” !” 这种。
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦

