需要 JS 文本插件动态参数问题
Require JS Text Plugin Dynamic Parameter Issues
我正在尝试编写一个函数,用于通过 Require 及其文本插件提取我的模板。
我希望能够调用 template('modules', 'login');
并让它加载所需的模板。
define(function(require) {
return function(path, file) {
return require('lib/text!templates/ + path + '/' + file + '.html');
}
});
所以我的代码会抛出错误,但如果我像这样硬编码路径
require(lib/text!templates/modules/login.html)
它按我想要的方式工作。我尝试了使用函数中的参数的不同变体来创建混合结果的 require 字符串参数,并记录了传递的字符串以确保它们相同。有什么想法吗?
这是我返回的错误
Uncaught Error: Module name "lib/text!templates/modules/profile.html" has not been loaded yet for context: _
您可以做您想做的事,但不能使用 同步 函数。
当您将 require(...)
与单个参数(字符串)一起使用时,您依赖于 RequireJS 对要求模块的 CommonJS 方式的支持。假设您在 require(...)
调用中放入了一个字符串文字,您说它有效:
define(function(require) {
return function(path, file) {
return require('lib/text!templates/modules/login.html');
}
});
在幕后,RequireJS 将上面的代码转换成这样:
define(['require', 'lib/text!templates/modules/login.html'], function(require) {
return function(path, file) {
return require('lib/text!templates/modules/login.html');
}
});
请注意 define
调用中添加的依赖项。当 require
执行时,您需要的模块已经加载,一切正常。你return作为你模块值的函数可以return同步'lib/text!templates/modules/login.html'
的值
问题是,如果 require(...)
调用包含 除字符串文字 之外的任何其他内容,此过程将无法运行。如果您设置 var x = 'lib/text!templates/modules/login.html'
然后执行 require(x)
, 它将不起作用! RequireJS 将无法像我上面显示的那样执行转换,当require(...)
执行,要加载的模块还没有加载,失败。这只是 RequireJS 的一个限制。 require(...)
以字符串作为唯一参数(而不是模块列表和回调)的形式 仅 支持加载模块的 CommonJS 方式。但是,如果尚未加载所需的模块,这种形式的 require
将失败 。这就是为什么 RequireJS 会转换出现此类调用的模块,以便将需要的模块添加到模块的依赖项列表中。但它可以对具有单个参数(字符串文字)的 require(...)
调用执行此转换 only。没有别的办法。
你可以做的是:
define(function(require) {
return function(path, file, cb) {
require(['lib/text!templates/' + path + '/' + file + '.html'], cb);
}
});
模块编辑的值 return 是一个 异步 函数,它将使用模板的值调用作为 cb
传递的回调。
我正在尝试编写一个函数,用于通过 Require 及其文本插件提取我的模板。
我希望能够调用 template('modules', 'login');
并让它加载所需的模板。
define(function(require) {
return function(path, file) {
return require('lib/text!templates/ + path + '/' + file + '.html');
}
});
所以我的代码会抛出错误,但如果我像这样硬编码路径
require(lib/text!templates/modules/login.html)
它按我想要的方式工作。我尝试了使用函数中的参数的不同变体来创建混合结果的 require 字符串参数,并记录了传递的字符串以确保它们相同。有什么想法吗?
这是我返回的错误
Uncaught Error: Module name "lib/text!templates/modules/profile.html" has not been loaded yet for context: _
您可以做您想做的事,但不能使用 同步 函数。
当您将 require(...)
与单个参数(字符串)一起使用时,您依赖于 RequireJS 对要求模块的 CommonJS 方式的支持。假设您在 require(...)
调用中放入了一个字符串文字,您说它有效:
define(function(require) {
return function(path, file) {
return require('lib/text!templates/modules/login.html');
}
});
在幕后,RequireJS 将上面的代码转换成这样:
define(['require', 'lib/text!templates/modules/login.html'], function(require) {
return function(path, file) {
return require('lib/text!templates/modules/login.html');
}
});
请注意 define
调用中添加的依赖项。当 require
执行时,您需要的模块已经加载,一切正常。你return作为你模块值的函数可以return同步'lib/text!templates/modules/login.html'
的值
问题是,如果 require(...)
调用包含 除字符串文字 之外的任何其他内容,此过程将无法运行。如果您设置 var x = 'lib/text!templates/modules/login.html'
然后执行 require(x)
, 它将不起作用! RequireJS 将无法像我上面显示的那样执行转换,当require(...)
执行,要加载的模块还没有加载,失败。这只是 RequireJS 的一个限制。 require(...)
以字符串作为唯一参数(而不是模块列表和回调)的形式 仅 支持加载模块的 CommonJS 方式。但是,如果尚未加载所需的模块,这种形式的 require
将失败 。这就是为什么 RequireJS 会转换出现此类调用的模块,以便将需要的模块添加到模块的依赖项列表中。但它可以对具有单个参数(字符串文字)的 require(...)
调用执行此转换 only。没有别的办法。
你可以做的是:
define(function(require) {
return function(path, file, cb) {
require(['lib/text!templates/' + path + '/' + file + '.html'], cb);
}
});
模块编辑的值 return 是一个 异步 函数,它将使用模板的值调用作为 cb
传递的回调。