RequireJS 中本地需求和全局需求的区别
Difference between local and global require in RequireJS
使用RequireJS
,我不相信我完全理解'require'的以下两种不同用法之间的区别。在这种情况下,我只是在谈论浏览器,而不是node.js。我的问题之一是:我可以使用 RequireJS 在前端同步要求某些依赖项吗?
首先是这个:
define(function(){
//below the require is definitely the global require of RequireJS, also aliased as requirejs.
require(['module'],function(mod){
//mod is loaded here only
}
});
然后就是这个:
define(['require'],function(require){
require(['dep'],function(dep){ //require is local not global
//dep is loaded here only
}
//now the require subsequently called is the 'local' require
var dep = require('dependency'); //I doubt this is possible on front-end..or is it?
});
我想我没有看到使用 require 作为参数在前端定义的目的,但是有吗?
我的困惑是因为这里的这个例子:
http://requirejs.org/docs/api.html
Relative module names inside define(): For require("./relative/name")
calls that can happen inside a define() function call, be sure to ask
for "require" as a dependency, so that the relative name is resolved
correctly:
define(["require", "./relative/name"], function(require) {
var mod = require("./relative/name");
});
从上面我只能猜测,如果一个模块相对于另一个模块,你可以在前端用RequireJS同步调用其他模块吗?
同样,这个例子:
Generate URLs relative to module: You may need to generate an URL that
is relative to a module. To do so, ask for "require" as a dependency
and then use require.toUrl() to generate the URL:
define(["require"], function(require) {
var cssUrl = require.toUrl("./style.css");
});
那么,我的问题是:local require 和 global require 有什么区别?对于 CommonJS 模块,本地要求显然不是 only/primarily。
所有这些示例都使用异步模块加载,而不是同步。您正在查看的代码有点令人困惑,因为它 似乎 是同步的,但实际上并非如此。
在这个例子中:
define(["require", "./relative/name"], function(require) {
var mod = require("./relative/name");
});
异步是显式的。因为模块 id './relative/name' 在依赖数组中,所以函数在(异步)加载之前不会被调用。 require 调用将立即 return 它已经(异步)加载的模块。
在另一种支持的语法中,隐藏了异步:
define(function(require) {
var mod = require("./relative/name");
});
但即使这样也是异步的。 Requirejs rewrites 这段代码包含一个依赖数组,就像上面的那个一样。所以异步就是你所得到的,尽管外表如此。
为什么这有用且与众不同?因为将数组的十几个成员与函数中将它们转换为变量的相应位置相匹配可能具有挑战性。考虑文档中的这个例子:
define([ "require", "jquery", "blade/object", "blade/fn", "rdapi",
"oauth", "blade/jig", "blade/url", "dispatch", "accounts",
"storage", "services", "widgets/AccountPanel", "widgets/TabButton",
"widgets/AddAccount", "less", "osTheme", "jquery-ui-1.8.7.min",
"jquery.textOverflow"],
function (require, $, object, fn, rdapi,
oauth, jig, url, dispatch, accounts,
storage, services, AccountPanel, TabButton,
AddAccount, less, osTheme) {
});
另一个有用且不同的原因是本地要求允许您正确解析相对模块路径。这使您可以定义一组彼此相关的模块,而无需明确指定它们所在的目录,如果该目录可能发生变化,这将很方便。
使用RequireJS
,我不相信我完全理解'require'的以下两种不同用法之间的区别。在这种情况下,我只是在谈论浏览器,而不是node.js。我的问题之一是:我可以使用 RequireJS 在前端同步要求某些依赖项吗?
首先是这个:
define(function(){
//below the require is definitely the global require of RequireJS, also aliased as requirejs.
require(['module'],function(mod){
//mod is loaded here only
}
});
然后就是这个:
define(['require'],function(require){
require(['dep'],function(dep){ //require is local not global
//dep is loaded here only
}
//now the require subsequently called is the 'local' require
var dep = require('dependency'); //I doubt this is possible on front-end..or is it?
});
我想我没有看到使用 require 作为参数在前端定义的目的,但是有吗?
我的困惑是因为这里的这个例子:
http://requirejs.org/docs/api.html
Relative module names inside define(): For require("./relative/name") calls that can happen inside a define() function call, be sure to ask for "require" as a dependency, so that the relative name is resolved correctly:
define(["require", "./relative/name"], function(require) {
var mod = require("./relative/name");
});
从上面我只能猜测,如果一个模块相对于另一个模块,你可以在前端用RequireJS同步调用其他模块吗?
同样,这个例子:
Generate URLs relative to module: You may need to generate an URL that is relative to a module. To do so, ask for "require" as a dependency and then use require.toUrl() to generate the URL:
define(["require"], function(require) {
var cssUrl = require.toUrl("./style.css");
});
那么,我的问题是:local require 和 global require 有什么区别?对于 CommonJS 模块,本地要求显然不是 only/primarily。
所有这些示例都使用异步模块加载,而不是同步。您正在查看的代码有点令人困惑,因为它 似乎 是同步的,但实际上并非如此。
在这个例子中:
define(["require", "./relative/name"], function(require) {
var mod = require("./relative/name");
});
异步是显式的。因为模块 id './relative/name' 在依赖数组中,所以函数在(异步)加载之前不会被调用。 require 调用将立即 return 它已经(异步)加载的模块。
在另一种支持的语法中,隐藏了异步:
define(function(require) {
var mod = require("./relative/name");
});
但即使这样也是异步的。 Requirejs rewrites 这段代码包含一个依赖数组,就像上面的那个一样。所以异步就是你所得到的,尽管外表如此。
为什么这有用且与众不同?因为将数组的十几个成员与函数中将它们转换为变量的相应位置相匹配可能具有挑战性。考虑文档中的这个例子:
define([ "require", "jquery", "blade/object", "blade/fn", "rdapi",
"oauth", "blade/jig", "blade/url", "dispatch", "accounts",
"storage", "services", "widgets/AccountPanel", "widgets/TabButton",
"widgets/AddAccount", "less", "osTheme", "jquery-ui-1.8.7.min",
"jquery.textOverflow"],
function (require, $, object, fn, rdapi,
oauth, jig, url, dispatch, accounts,
storage, services, AccountPanel, TabButton,
AddAccount, less, osTheme) {
});
另一个有用且不同的原因是本地要求允许您正确解析相对模块路径。这使您可以定义一组彼此相关的模块,而无需明确指定它们所在的目录,如果该目录可能发生变化,这将很方便。