2013-05-18 31 views
-1

我想在我的項目中使用RequireJs,但是我發現我不能寫代碼如下如果我使用RequireJs,所有js函數都需要.js文件嗎?

<html> 
    <head> 
    </head> 
    <body> 
     <input type="button" /> 
     <script> 
      // Use the jQuery 
      $(function() { 
       //some code 
      } 
      ) 
     </script> 
     <input /> 
     <input /> 

     // I had added the jQuery in required shim and jQuery is working in RequireJs. 
     <script data-main="scripts/main" src="/scripts/require.js"></script> 
    </body> 
</html> 

出現錯誤$ is undefined,加載頁面時。 我擡頭看了很多文章,但我解決不了。我該如何處理?我不想將示例js函數移動到.js文件。

+2

include user2361114

+0

否則包含在頭部分 – user2361114

+0

如果您不打算在頭部腳本標記中使用jQuery,那麼您只需要$(document).ready事件偵聽器。 然後你可以做'document.addEventListener('DOMContentLoaded',function(){//一些代碼})' 如果你想支持

回答

0

第一件事情第一件$在JavaScript中沒有任何意義,但被流行的jQuery library使用。 JavaScript !== jQuery

其次,假設您已通過require.js加載jQuery,則需要在 <script data-main="scripts/main" src="/scripts/require.js"></script>之後放置您的初始腳本標記,因爲它正在require.js之前調用。

第三,require.js異步加載文件,因此您必須將代碼封裝在require()define()標記中,並附帶一系列依賴關係,以便只在加載後才調用它們。

例如

<html> 
    <head> 
     <script data-main="scripts/main" src="/scripts/require.js"></script> 
    </head> 
    <body> 
     <input type="button" /> 
     <script> 
      /* 
      RequireJS is asynchronous so you must wrap your code 
      in "require(["jQuery"], ...)" so it only calls it once 
      jQuery has been loaded. Each included script is available 
      to you as a parameter in the callback function. 
      */ 
      require(["jQuery"], function($) { 
       $(function() { 
       }); 
      }); 
     </script> 
     <input /> 
     <input /> 
    </body> 
</html> 

您的設置應該是這樣的:

requirejs.config({ 
    paths: { 
     'jQuery': 'path/to/jquery' 
    }, 
    shim: { 
     'jQuery': { 
      exports: '$' 
     } 
    } 
}); 
+0

謝謝你的回答。但是,如果js代碼縮小,那對我並不好。我必須兩次包含jQuery。 'main.js'是一個,在頁面中是兩個。 – user2306785

+0

@ user2306785這不包括它兩次,縮小不影響任何東西(它仍然是相同的代碼)。您將它包含在main.js中並使用'require()'告訴您的腳本等待它加載並確保您的代碼被正確使用它的範圍。你沒有加載它兩次。 –

+0

如何告訴'require()'在main.js中查找'jQuery'代碼,因爲所有.js文件都合併爲一個文件。 – user2306785

0

您還必須包含jQuery。有一個whole manual page致力於requirejs.org這應該幫助你。

也許你想要的也是解釋here。從那裏採取:

require(["helper/util"], function(util) { 
    //This function is called when scripts/helper/util.js is loaded. 
    //If util.js calls define(), then this function is not fired until 
    //util's dependencies have loaded, and the util argument will hold 
    //the module value for "helper/util". 
}); 
0

Require.js甚至不實現$。爲什麼你甚至包裝你在$(..)中的功能?

+0

不僅如此,他還在require.js加載之前調用'$()'。 –

+0

當然,但沒有requirejs實現$它並不重要 – MofX

+0

對不起,我沒有解釋我的問題的細節,我想在我的示例代碼中使用jQuery。 – user2306785

0

嘗試在requirejs腳本標記之後移動示例腳本標記。目前,您的示例函數在requirejs有機會加載jQuery之前執行。

+0

我試圖在中移動'',但它不起作用。 – user2306785

+0

我想你需要把你的示例函數也包裝在'require([「jquery」],function($){..})'(或者它是'define',我總是讓這些混合起來)讓requirejs知道你需要jQuery,否則它可以決定不加載它。 –

1

正如你在你的問題中寫道:

所有的JS功能需要在.js文件,如果我使用requirejs?

而且隨着RequireJS的定義:

RequireJS是一個JavaScript文件和模塊加載器。 使用像RequireJS這樣的模塊化腳本加載器可以提高代碼的速度和質量。

所以做這樣的插入內嵌<script>代碼會在頁是不是一個好主意

然而,在回答你的問題,因爲RequireJS加載腳本asynchronously,所以你不知道什麼時候jQuery將加載,不能使用內嵌腳本老學校,你需要使用definerequire方法爲George Reith寫在the answer

+0

我認爲'定義'不好,你在這裏檢查:http://requirejs.org/docs/errors.html#mismatch – user2306785

+0

@ user2306785是的,你是對的:) – 2013-05-18 09:12:35

相關問題