在最新的firefox 21.0中使用requireJS加载多个模块失败 [英] loading multiple modules using requireJS in latest firefox 21.0 fails

查看:105
本文介绍了在最新的firefox 21.0中使用requireJS加载多个模块失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下定义的3个差异模块

I tried with 3 diff modules as defined below

credit.js

credit.js

        define(function(){
            return{
                getCredits: function (){
                    console.log("Inside getCredits");
                    return 10;
                }
              }
        });

product.js

product.js

        define(function(){
          console.log("Inside product");
          return {
            bookTheProduct: function(){
            console.log("Inside bookTheProduct");
            return true;
          }
         }
        });

pruchase.js

pruchase.js

        require.config({
            shim: {
                purchase: {
                    deps : ["credit","product"]
                }
            }
        });
        define(["credit","product"], function(credit,product){
            console.log("purchaseproduct");
            return {
                 purchaseProduct: function (){
                     console.log("Inside of PurchaseProduct");
                     var credits = credit.getCredits();
                     if(credits > 0){
                         product.bookTheProduct();
                         return true;
                     }
                     return false;
                 }
            }
        });

在app.js中使用它

used it in app.js

        require(["purchase"],function(purchase){
          purchase.purchaseProduct();
        })

在firefox 21.0中试过这个,在加载购买时它加载了信用模块,但从未加载过产品模块。如果我反转订单,它会加载产品模块,但不会加载信用模块。在RequireJs文档和mozilla文档中找不到任何帮助。也没有看到任何人碾压它。有没有人遇到过这个问题?我做错了什么,如果可以,请指出我的错误。
谢谢

Tried this in firefox 21.0 , while loading purchase it loaded credit module but never loaded product module . If i reverse the order it loads the product module but not the credit module . Didn't find any help in RequireJs documentation nor in mozilla documentation . Also didn't see anyone cribing abt it . did any body ever faced this problem ? I am doing some thing wrong , if so can you please point me the mistake . thanks

推荐答案

如评论中所述, shim 配置适用于不支持AMD模块定义的Javascript文件,用于导入例如将一些变量添加到窗口范围(如jQuery)的javascript库。了解更多关于 shim 此处

As mentioned in the comments, the shim configuration is for Javascript files that do not support AMD module definitions and is used to import for example javascript libraries that add some variable to the window scope (like jQuery). Read more about shim here.

在您的情况下,您的所有模块都是AMD模块(它们都是使用 define 定义的) ,所以 shim 是没有必要的。相反,您可以使用路径配置为模块创建别名。同时将 require.config 移出模块并进入 require 调用的位置是值得推荐的。

In your case, all your modules are AMD modules (they are all defined using define), so shim is not necessary. Instead you can just use the paths configuration to create aliases for your modules. Also moving your require.config out of the modules and into where your require call happens is recommendable.

因此,从purchase.js文件中删除 require.config ,然后将以下内容添加到应用程序的开头。 js -file

So, remove the require.config from your purchase.js -file, then add the following to the beginning of your app.js -file

require.config({
  // with the paths -config, you create aliases for you modules
  // basically you tell require that this file contains definition for module x
  paths: {
    'purchase': 'path/to/purchase', // no .js file ending in the path
    'credit': 'path/to/credit',
    'product': 'path/to/product'
  }
});

您可以阅读有关require.config的更多信息这里

You can read more about require.config here.

现在你已经配置了RequireJS,它知道模块的位置购买,信贷和产品的位置,它将加载它们。在此之后,在声明RequireJS依赖项时,可以使用它们各自的别名引用它们。

Now you have configured RequireJS so, that it knows where the modules purchase, credit and product are located and that it will load them. After this they can be referenced with their respective aliases when declaring RequireJS dependencies.

希望这会有所帮助!

这篇关于在最新的firefox 21.0中使用requireJS加载多个模块失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆