zoukankan      html  css  js  c++  java
  • 小程序-模块化

    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");

  • 相关阅读:
    缺省源
    MySQL曹操外卖一
    MySQL曹操外卖二
    正确使用MySQL外键约束
    html大部分实用标签(结构型)
    html超级基础标签
    我的简单作业
    章节课程复习笔记
    FC超级玛丽研究(NES游戏)
    二维码生成
  • 原文地址:https://www.cnblogs.com/wuqilang/p/12076131.html
Copyright © 2011-2022 走看看