我正在使用RequireJS的text
插件。是否可以在路径配置文件中引用文本文件?我试过了
require.config({
paths: {
'myTemplate': 'text!templates/myTemplate.html'
}
});
但这没效果。
答案 0 :(得分:7)
它不起作用的原因是因为RequireJS插件被设计为用作require命令的一部分,而不是在配置中。
尝试:
require.config({
paths: {
'myTemplate': 'templates/myTemplate.html'
}
});
并在您的模块中:
define(
['text!myTemplate'],
function () {}
)
答案 1 :(得分:4)
RamenRecon的答案有所帮助,但在我的情况下,我认为使用myTemplate作为路径和模板名称有点令人困惑。我发现的关键是只替换路径,而不是实际的文件名。因此,要使用require和路径配置将路径抽象为/subSystem/templates/myTemplate.htm,请按以下方式设置配置:
require.config({
paths: {
templatePath: 'subsystem/templates'
}
});
然后在你的模块定义中:
define(['text!templatePath/myTemplate.htm'],
function(template) {}
);