2015-06-07 80 views
6

要求js工作正常,沒有捆綁。但是,無論何時使用捆綁包,我都會嘗試導入模塊超時。RequireJS模塊加載超時使用捆綁

這裏是我如何建立一個與asp.net的MVC捆紮機/束minifier

bundles.Add(new ScriptBundle("~/bundles/test").Include(
      "~/scripts/jquery-{version}.js", 
      "~/scripts/bootstrap.js", 
      "~/scripts/moment.js")); 

bundles.EnableOptimizations = true; 

繼承人是需要的js配置在CSHTML文件:

<script> 
    require.config({ 
     baseUrl: "/scripts", 
     paths: { 
      jquery: "jquery-1.11.2" 
     }, 
     waitSeconds: 20, 
     shim: { 
      bootstrap: { 
       deps: ['jquery'] 
      } 
     }, 
     bundles: { 
      '@Scripts.Url("~/bundles/test").ToString()': [ 
       'jquery', 
       'bootstrap', 
       'moment' 
      ] 
     } 
    }); 
    require(["app/main/test"]); 
</script> 

而爲的JS page(app/main/test):

require(['jquery', 'moment'], function ($, moment) { 
    console.log(moment().format()); 
}); 

jquery,bootstrap和moment庫在測試包中我有cre ated,但我得到加載超時加載頁面的時刻。

這裏的Chrome檢查錯誤:

load timeout

任何想法?

在此先感謝。

回答

0

這是因爲你根本不需要你的包。你的需求調用只有jQuery和時刻。您提供了jquery文件路徑,因此requirejs使用該路徑下載並提供jquery模塊。但是由於目前沒有路徑定義,它只是您創建的軟件包的一部分。所以requirejs嘗試通過它的模塊名稱下載時刻作爲路徑,從而引發錯誤。

一個簡單的解決方法是需要捆綁本身。

require(['@Scripts.Url("~/bundles/test").ToString()'], function(bundle){ 
    //At this point jquery, moment and bootstrap will be loaded. 
}); 

您可以選擇直接使用jQuery,從全局命名空間時刻上面的例子,或者你可以嘗試seperately在要求他們下面的例子。我不確定,但由於循環依賴性,您可能會在下面的示例中出現錯誤。

require(['@Scripts.Url("~/bundles/test").ToString()', 'jquery', 'moment'], function(bundle, $, moment){ 
     //At this point jquery, moment and bootstrap will be loaded. 
    }); 
0

剛剛從你的包

bundles.Add(new ScriptBundle("~/bundles/test").Include(
     "~/scripts/bootstrap.js", 
     "~/scripts/moment.js")); 
... 
bundles: { 
    '@Scripts.Url("~/bundles/test").ToString()': [ 
     'bootstrap', 
     'moment' 
    ] 
} 
... 

你已經擁有它的路徑指定刪除'jquery'

paths: { 
    jquery: "jquery-1.11.2" 
}, 

這似乎s require.js將模塊映射到捆綁包,以便一旦加載了捆綁包的模塊,該捆綁包就不會再次加載。