We often see define and require in requireJS, but what's their difference? I try to elaborate as below.
require
- When you want to load some stuff, you need to use require, like jquery, bootstrap etc.
- It has NO return statement
require(["jquery", "bootstrap", "bootstrap_select"], function() {
//requirejs sugar function
var angular = require('angular');
});
define
- As the name implies, it helps you define module that will be depend by other modules or applications
- It has return statement for inclusion by other modules
define(["angular", "angular-route", "angular-animate"],function(angular){
var insiderApp = angular.module("insiderApp",["ngRoute", "ngAnimate"]);
//routing
insiderApp.config(['$routeProvider','$locationProvider',function($scope,$routeProvider,$locationProvider){
}]);
return insiderApp;
});
This is official explanation
/**
* The function that handles definitions of modules. Differs from
* require() in that a string for the module should be the first argument,
* and the function to execute after dependencies are loaded should
* return a value to define the module corresponding to the first argument's
* name.
*/
沒有留言:
張貼留言