「小程序JAVA实战」 小程序抽离公用方法进行模块化(12)

发布时间:2020-05-21 22:48:55

来源:51CTO

阅读:273

作者:IT人故事

小程序的模块化,把砖磊成一个墩子,用的时候把整个墩子移走。js更好的调用,应用更加公用化。源码:https://github.com/limingios/wxProgram.git 中的No.7

小程序的模块化抽离通用方法作为通用函数

构建utils-common类官方的阐述

>https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html

bd130169c1d22fb013cd1ca42fb15e4b.png程序演示

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 = sayGoodbye

6cfc89aa172a5cfe6cd36eadc5516c8c.png

PS:需要注意的是console.log(`Goodbye ${name} !`)

console.log("Goodbye " + name + " !")

区别如果用了 ${} 最外层需要用“符号,如果你喜欢老套路可以按照我的 “Goodbye ” + name + ” !” 这种。

>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!

>>原文链接地址:「小程序JAVA实战」 小程序抽离公用方法进行模块化(12)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐