This article mainly introduces how to use mini-define to implement modular Management of front-end code. It is a very good article. We recommend it to partners who need it.
Mini-define
A simple front-end modular framework based on require. If you don't want to spend time learning require. js or look at the long cmd/amd specifications, then this mini-define is your good choice. If you have used sea. js or require. js before, mini-define is more efficient, lightweight, and easy to use. Project address: github
Usage
First define the module
Definition Module
I. Use the define function to define a module
1.1 depending on whether there is dependency, there are two cases:
1.1.1: no dependent Module
The Code is as follows:
Define ('id', function (){
// Put your code here
});
1.1.2: Dependent modules
The Code is as follows:
Define ('id', ['modea ', 'modeb'], function (A, B ){
// Put your code here
});
1.2 if you want to return the processing result for external use, there are two situations:
1.2.1 returned objects:
The Code is as follows:
Define ('id', function (){
Return {
// Put your code here
}
});
1.2.2 no objects returned
The Code is as follows:
Define ('id', function (){
// Put your code here
});
Ii. Use the require () function to call the module.
2.1 depending on the number of requested modules, there are two possible cases:
2.1.1. Call a single module
Require ('modeid ')
2.1.2. Call multiple modules
Require (['modea ', 'modeb']);
2.2 Based on whether callback processing exists, there are two possible scenarios:
2.2.1 callback Handler
The Code is as follows:
Require ('modeid', function (mode ){
// Put your code here
});
Require (['modea ', 'modeb'], function (A, B ){
// Put your code here
});
2.2.2 no callback Processing
Require ('modeid ');
Reference the required modules on the index.html page.
The Code is as follows: