【问题标题】:Getting the content of text files loaded with RequireJS?获取使用 RequireJS 加载的文本文件的内容?
【发布时间】:2013-07-18 09:26:49
【问题描述】:

我已经在使用RequireJS加载jQuery等插件了,如下:

var dependencies = ["order!jquery", "order!plugins/jquery.typewatch",  
   "order!plugins/jquery.csv-0.71.min", "plugins/jquery.zclip.min"];
require(dependencies, function($) {
   // jQuery code goes here
});

这很好用。我现在想使用RequireJS来加载一个文本文件,所以我下载了RequireJS text plugin,并修改了我的代码:

var dependencies = ["order!jquery", "order!plugins/jquery.typewatch",  
   "order!plugins/jquery.csv-0.71.min", "plugins/jquery.zclip.min",
   "text!data/mytext.txt"];
require(dependencies, function($) {
   // jQuery code goes here
});

查看 DevTools,请求工作正常 - 正在请求文本文件。但是如何获取文本文件的内容

我试过这个,正如 RequireJS 文档所建议的那样:

require(dependencies, function($, txt) {
   console.log(txt);
});

txt 未定义。

更新:如果相关,我正在使用 RequireJS 1.0.7,并从我的 HTML 中按如下方式调用它:

<script data-main="scripts/main.js" src="/scripts/require-jquery.js"></script>

【问题讨论】:

    标签: javascript requirejs amd js-amd


    【解决方案1】:

    每个函数参数对应于加载依赖项的结果,在依赖项列表中指定。您需要在函数中声明与依赖项一样多的参数,或者将 text!data... 依赖项移到前面:

    var dependencies = ["order!jquery", "text!data/mytext.txt", ....];
    require(dependencies, function($, txt) {
        console.log(txt);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 2010-11-02
      • 2013-02-02
      相关资源
      最近更新 更多