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. solr 分析器

    源https://www.w3cschool.cn/solr_doc Solr 分析器被指定为 schema.xml 配置文件中的<fieldType>元素的子元素(在与 solrconf ...

  2. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  3. [第一波模拟\day3\T2]{独立集}(bubble.cpp)

    [问题描述] 有一天,一个名叫顺旺基的程序员从石头里诞生了.又有一天,他学会了冒泡排序和独立集.在一个图里,独立集就是一个点集,满足任意两个点之间没有边.于是他就想把这两个东西结合在一起.众所周知,独 ...

  4. 【02】使用Firebug查看和编辑HTML和CSS

    使用Firebug查看和编辑HTML和CSS 描述 在本章节的教程中,我们将讨论如何使用Firebug查看和编辑HTML和CSS. 使用Firebug查看和编辑HTML 在你要查看的元素上右击鼠标然后 ...

  5. luogu3313 [SDOI2014]旅行

    对每一个宗教建一棵线段树,然后树剖搞搞 #include <iostream> #include <cstdio> using namespace std; int n, m, ...

  6. 【转】阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访

    原文链接:http://www.iteye.com/magazines/103   Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ ...

  7. Codeforces Round #387 (Div. 2) A+B+C+D!

    A. Display Size 水题,暴力(数据都是水题).0:04 int main() { int n; while(~scanf("%d",&n)) { int mi ...

  8. UITableView点击背景

    系统自定义的点击背景有时间觉得效果不好想换个 - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelect ...

  9. [codeforces538F]A Heap of Heaps

    [codeforces538F]A Heap of Heaps 试题描述 Andrew skipped lessons on the subject 'Algorithms and Data Stru ...

  10. Codeforces 892 B.Wrath

    B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...