Giter VIP home page Giter VIP logo

js-base-module's Introduction

JS 实现简单模块化

1.介绍

你的传统开发项目是不是还存在于这种方式来引入模块呢?

本项目借助于Layui模块化的原理来实现

1.1例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="content">
        内容
    </div>

    <!-- 引入的模块 -->
    <script src="module1.js"></script>
    <script src="module2.js"></script>
    <script src="module3.js"></script>
    <script src="module4.js"></script>
    <script src="module3n.js"></script>
</body>
</html>

1.2更改后

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./core.js"></script>
</head>
<body>
    <button id="add-inner-btn">添加Inner模块</button>
    <button id="add-outter-btn">添加Outter模块</button>
    <script>
         /**
         * 使用说明:
         * const $mo = JsModule.create({
         *      moduleBaseUrl: "./js-modules/"
         * });
         * 
         * 1.定义模块
         * $mo.define(function (exports) {
         *      exports(模块名,模块内容);
         * });
         * 
         * 
         * 2.使用模块
         * $mo.use(模块名,function () {
         *      do sth...
         * });
         */

        
        //创建一个实例
        const $mo = LayuiJsModule.create({
            moduleBaseUrl: "./js-modules/"
        });

        //分别使用了,inner、bind-event模块
        $mo.use(["inner", "bind-event"], function ($) {
            console.log($)
            console.log("我已经加载好了模块了")
        });
    </script>
</body>
</html>

2.使用说明

2.1 引入核心文件

<script src="./core.js"></script>

2.2创建一个模块实例

//创建一个实例
const $mo = JsModule.create({
  	moduleBaseUrl: "./js-modules/" //模块对应的跟地址
});

2.3创建一个模块

//定义模块
$mo.define(function(exports) {
    let exportData = {
        name: "Outter Module",
        create: "2020-10-23",
        methods: {
            getUserTableList() {
                return [1, 2, 3, 4, 5]
            }
        }
    }

    //模块名称, 模块内容
    exports("outter", exportData)
});

2.4使用模块

//分别使用了outter模块
$mo.use(["outter"], function ($) {
  	console.log($); //导入的模块
  	console.log("我已经加载好了模块了")
});

js-base-module's People

Contributors

menghaor avatar

Stargazers

 avatar 芹菜 avatar  avatar x-li avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.