javascript 加载 requirejs 和 jquery 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12983841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:33:26  来源:igfitidea点击:

Getting Error with loading of requirejs and jquery

javascriptjqueryrequirejs

提问by whatf

I am trying a simple demo with requirejs and jquery for AMD. Following is my folder structure structure:

我正在为 AMD 尝试一个带有 requirejs 和 jquery 的简单演示。以下是我的文件夹结构结构:

├── index.html
└── js
    ├── lib
    │?? ├── jquery.js
    │?? └── require.js
    └── main.js

in my index.html file i have the following content:

在我的 index.html 文件中,我有以下内容:

  <head>
    <script data-main="js/main" src="js/lib/require.js"></script>
  </head>

  <body>
    <div id="hello">    </div>

  </body>

and my main.js file looks like this:

我的 main.js 文件如下所示:

define(['lib/jquery'], function ($) {

  $("#hello").html("Wow this works!");

});

But when i do, i get an error: Uncaught TypeError: undefined is not a functionin main.js line 3.

但是当我这样做时,我收到一个错误:Uncaught TypeError: undefined is not a function在 main.js 第 3 行。

What is wrong? I can't understand?

怎么了?我不明白?

采纳答案by roomcays

I have tried your example without "$" in first line of main.js and it worked properly.

我已经在 main.js 的第一行尝试了没有“$”的例子,它工作正常。

Fixed main.js:

固定main.js

define(['lib/jquery'], function () {
     $("#hello").html("Wow this works!");
});

回答by Behrang

Read this issue: https://github.com/jrburke/requirejs/issues/435

阅读本期:https: //github.com/jrburke/requirejs/issues/435

My understanding is this is because of how jquery defines itself. It is using a named define call:

我的理解是这是因为 jquery 如何定义自己。它使用命名的定义调用:

define( "jquery", [], function () { return jQuery; } );

So now, if you require 'lib/jquery'it won't work. You have to exactly require 'jquery'for it to work.

所以现在,如果你需要'lib/jquery'它就行不通了。你必须完全要求'jquery'它才能工作。

EDIT:

编辑:

If you want to put jquery in lib/folder and your base url to be the parent folder of lib/(lib/../) you can use a shim like this:

如果您想将 jquery 放在lib/文件夹中,并且您的基本 url 是lib/( lib/../)的父文件夹,您可以使用这样的垫片:

requirejs.config({
  baseUrl: 'js',
  shim: {
    'lib/backbone': {
      deps: ['lib/underscore', 'lib/jquery'],
      exports: 'Backbone'
    },
    'lib/underscore': {
      exports: '_'
    },
    'lib/jquery': {
      exports: '$'
    }
  }
});

requirejs(['lib/jquery'], function($) {
  // use $
});

回答by Nathan Black

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

is what you want. the data-mainattribute just points to what should be the root of relative request for it. So you'll need to load in main directly or through an script tag or a require()call in an inline script block

是你想要的。该data-main属性只是指向它的相对请求的根应该是什么。因此,您需要直接加载 main 或通过脚本标记或require()调用内联脚本块