小程序-模块化

require

  any require(string path)
  引入模块。返回模块通过module.exports或exports暴露的接口。

参数
名称 类型 说明
path string 需要引入模块文件相对于当前文件的相对路径,或npm模块名,或npm模块路径。不支持绝对路径

示例代码:
复制代码
            // common.js
            function sayHello(name){
                console.log(`hello ${name}`)
            }
            function sayGoodbye(name){
                console.log(`goodbye ${name}`)
            }
            module.exports.sayHello=sayHello
            exports.sayGoodbye=sayGoodbye

            var common=require("common.js")
            Page({
                helloMINA:function(){
                    common.sayHello("MINA")
                },
                goodbyeMINA:function(){
                    common.sayGoodbye("MINA")
                }
            })
复制代码

module

    当前模块对象

属性
属性 类型 说明
exports Object 模块向外暴露的对象,使用require引用该模块时可以获取

示例代码:
复制代码
            // common.js
            function sayHello(name){
                console.log(`hello ${name}`)
            }
            function sayGoodbye(name){
                console.log(`goodbye ${name}`)
            }

            module.exports.sayHello=sayHello
            exports.sayGoodbye=sayGoodbye
复制代码

exports

    module.exports的引用

示例代码:
复制代码
            // common.js
            function sayHello(name){
                console.log(`hello ${name}`)
            }
            function sayGoodbye(name){
                console.log(`goodbye ${name}`)
            }

            module.exports.sayHello=sayHello
            exports.sayGoodbye=sayGoodbye
复制代码

 

导出:

  module.exports={};

引入:

  const util=require("../../utils/util");

posted @   吴小明-  阅读(535)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示