【问题标题】:Using a string as a parameter in requirejs text plugin在requirejs文本插件中使用字符串作为参数
【发布时间】:2013-05-15 07:41:44
【问题描述】:

我想在 require 中使用一个字符串并加载一个车把模板文件。但我总是得到

"Invalid require call : notloaded",'Module name "'+i+'" 尚未加载上下文:"


define(function( require) {

  var getTemplateFile = function(templateName) {
     return require(['text!../html/templates/header.tpl']);   /* This works */
  }

});


//String Concatenated
define(function( require) {

  var getTemplateFile = function(templateName) {
     return require(['text!../html/templates/'+templateName+'.tpl']);   /* Does not work */
  }

});

我遵循了此处给出的解决方法 (RequireJS text plugin and variable concatenated string),但我仍然遇到了同样的错误。 任何帮助使其正常工作。

【问题讨论】:

    标签: javascript requirejs


    【解决方案1】:

    您误用了 require 函数。见here for the spec。您需要使用require(String)(连同数组版本)或仅使用require(Array, Function),具体取决于您想要实现的目标。您链接到的答案使用数组变体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2016-07-26
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 2020-10-31
      • 2012-05-17
      相关资源
      最近更新 更多