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>

 

ES6 模块化(Module)export和import详解 export default的更多相关文章

  1. JS ES6中export和import详解

    1.Export 模块是独立的文件,该文件内部的所有的变量外部都无法获取.如果希望获取某个变量,必须通过export输出, // profile.js export var firstName = ' ...

  2. ES6模块之export和import详解

    ES6中的模块即使一个包含JS代码的文件,在这个模块中所有的变量都是对其他模块不可见的,除非我们导出它.ES6的模块系统大致分为导出(export)和导入(import)两个模块. 1.模块导出(ex ...

  3. [js高手之路] es6系列教程 - 对象功能扩展详解

    第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...

  4. require 和 import 详解

    前言 JS模块化编程是前端小伙伴们必不可少的知识,下面妹子将于自认为比较清晰的方式列举出来. 1 require 特点: 1.运行时加载 2.拷贝到本页面 3.全部引入 1.1 CommonJS No ...

  5. css模块化及CSS Modules使用详解

    什么是css模块化? 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好的可管理模块的方 ...

  6. [ES6系列-03]ES6中关于参数相关特性详解(参数默认值与参数解构赋值与剩余参数)

    [原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 今天总结一下 ES6 中跟参数相关的内容. 欢迎补充斧正.留言交流. 让我们互相学习一起进步. 1. ES6 参数默认值( ...

  7. Go依赖模块版本之Module避坑使用详解

    前提 对于Go的版本管理主要用过 glide,下面介绍 Go 1.11 之后官方支持的版本管理工具 mod. 关于 mod 官方给出了三个命令 go help mod.go help modules. ...

  8. Python Import 详解

    http://blog.csdn.net/appleheshuang/article/details/7602499 一 module通常模块为一个文件,直接使用import来导入就好了.可以作为mo ...

  9. python import详解

    1.import作用 引入模块 2.import的特点 一个程序中,import的模块不会重复被引用,如: # test1.py import test2 print test2.attr # tes ...

随机推荐

  1. 第三天,小作业,表达式,while循环

    num += 1 等价于 num = num + 1num -= 1 等价于 num = num - 1num *= 2 等价于 num = num * 2num /= 2 等价于 num = num ...

  2. tomcat——大致简介和执行过程

    jsp简介 JSP: JAVA Server Page 使用JAVA语言编写的一种在服务器运行的动态页面 JSP = JAVA + HTML JSP 的执行过程 1: 翻译阶段 把JSP源文件翻译成 ...

  3. python的unittest单元测试框架断言整理汇总

    自动化脚本最重要的是断言,正确设置断言以后才能帮助我们判断测试用例执行结果. 一.先说说unittest常用的断言吧 常用的就以下几个,网上一搜一大堆.python版本2.7以上都可以调用了. 断言语 ...

  4. 大数据学习——flume安装部署

    1.Flume的安装非常简单,只需要解压即可,当然,前提是已有hadoop环境 上传安装包到数据源所在节点上 然后解压  tar -zxvf apache-flume-1.6.0-bin.tar.gz ...

  5. VIM键盘图

  6. [codeforces724D]Dense Subsequence

    [codeforces724D]Dense Subsequence 试题描述 You are given a string s, consisting of lowercase English let ...

  7. zju 3209 dancing links 求取最小行数

    题目可以将每一个格子都看做是一列,每一个矩形作为1行,将所有格子进行标号,在当前矩形中的格子对应行的标号为列,将这个点加入到十字链表中 最后用dlx求解精确覆盖即可,dance()过程中记得剪枝 #i ...

  8. 贪婪大陆(cogs 1008)

    [题目描述] 面对蚂蚁们的疯狂进攻,小FF的Tower defense宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海,前方是变异了的超级蚂蚁. ...

  9. C++内存分配方式(——选自:C++内存管理技术内幕)

    C++内存分配的区: 1.栈:程序运行时分配的,局部变量,以及传入的参数等存储的地方,在程序结束的时候会回收 2.堆:new分配,由delete释放 3.自由存储区:malloc分配 4.全局/静态存 ...

  10. Max Sum Plus Plus-HDU1024(dp)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...