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


    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>






     
  • 相关阅读:
    第十二章 基本数据类型
    第十一章 变量名的力量
    第十章 使用变量的一般事项
    第九章 伪代码编程过程
    第八章 防御式编程
    JMeter简介
    第七章 高质量的子程序
    第六章 可以工作的类
    第五章 软件构建中的设计
    第四章 关键的“构建”决策
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9250769.html
Copyright © 2020-2023  润新知