ES6 模块化(Module)export和import详解 - CSDN博客 https://blog.csdn.net/pcaxb/article/details/53670097

微信小程序笔记<六>模块化 —— module.exports - MirageFireFox - 博客园 https://www.cnblogs.com/MirageFox/p/7905724.html

const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
} const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
} var chkStrLength = (str, minLen) => {
if (str.length < minLen) {
return true
}
return false
} module.exports = {
formatTime: formatTime,
chkStrLength: chkStrLength
}

  

var util = require('../../utils/util.js');
var chkStrLength = util.chkStrLength;
Page({
onLoad: function(option) {
console.log("加载用户中心页面,判断是否需要登录")
//校验逻辑:参数是否合法、网络环境是否异常(是否非历史地市)
console.log(wx.getStorageSync("username"))
console.log(chkStrLength(wx.getStorageSync("username"), 1))
console.log(123)

  

模块化 · 小程序 https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/module.html

模块化

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

需要注意的是:

  • exports 是 module.exports 的一个引用,因此在模块里边随意更改 exports 的指向会造成未知的错误。所以更推荐开发者采用 module.exports 来暴露模块接口,除非你已经清晰知道这两者的关系。
  • 小程序目前不支持直接引入 node_modules , 开发者需要使用到 node_modules 时候建议拷贝出相关的代码到小程序的目录中。
api.getNewsList
 
import api from '../api/api'
 
 
 
 
import wepy from 'wepy'

const notShorterStr = (str, minLen) => {
if (str.length < minLen) {
return false
}
return true
}
const isLogined = () => {
const username = wx.getStorageSync('username')
const uid = wx.getStorageSync('uid')
const gid = wx.getStorageSync('gid')
if (notShorterStr(username, 4) && notShorterStr(uid, 1) && notShorterStr(gid, 1)) {
return true
}
return false
} export default {
notShorterStr,
isLogined }
<script>
import wepy from 'wepy'
import api from '../api/api'
import util from '../utils/util'
export default class userCenter extends wepy.page {
config = {
navigationBarTitleText: '我',
enablePullDownRefresh: false
}
data = {
localImgPath: ''
}
onLoad(option) {
this.localImgPath = api.localImgPath
this.$apply()
const isLogined = util.isLogined()
if (!isLogined) {
wx.reLaunch({
url: './userLogin'
})
}
}
exitThis(e) {}
onShow(option) {}
onShareAppMessage() {}
methods = {}
}
</script>

 

最新文章

  1. dojo tree edit的使用[前端]
  2. 实现type函数用于识别标准类型和内置对象类型
  3. [codevs1141]数列
  4. hdu 1279 验证角谷猜想
  5. 如何在C#中获得input文本框中的值
  6. html/CSS基础知识回顾
  7. ubuntu安装无线驱动
  8. Android Launcher 怎样去掉主菜单,全部应用摆在桌面,相似小米桌面
  9. uva_1422 Processor
  10. cgLib生成动态代理
  11. 制作Windows服务项目详细攻略
  12. php 抽奖概率 随机数
  13. 配置CLion作为Qt5开发环境
  14. 题解 P1801 【黑匣子_NOI导刊2010提高(06)】
  15. 各大型网站架构分析收集-原网址http://blog.csdn.net/lovingprince/article/details/3379710
  16. sklearn11_函数汇总
  17. openstack学习-glance安装(三)
  18. 修改firefox/chrome浏览器的UserAgent
  19. 使用Lotus Enterprise Integrator (LEI)将Domino附件移至关系数据库(图文过程)
  20. Implementation:Dijkstra

热门文章

  1. 【转】Unable to load native-hadoop library for your platform(已解决)
  2. 杭电 4907 Task schedule &#183;
  3. 【51nod 1791】 合法括号子段
  4. Mac OS X 上如何切换默认的 Python 版本?
  5. HDU1412-{A} + {B},通过率并不高,但同样是用一个很简洁的函数unique,超短代码水过~
  6. 【Kubernetes】Kubernetes的Service外部访问方式:NodePort和LoadBalancer
  7. [BZOJ3052][UOJ#58][WC2013]糖果公园
  8. Monkey King(左偏树)
  9. Scrambled Polygon--poj2007(极角排序模板)
  10. strcpy c标准库函数