ES6 模块化(Module)export和import详解 export default

2022-12-15,,,,

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的相关教程结束。

《ES6 模块化(Module)export和import详解 export default.doc》

下载本文的Word格式文档,以方便收藏与打印。