Frage

Here is my folder structure:

project
|---src
|   |---lib/require.js
|   |---object/extend.js
|   |---main.js
|
|---index.html

in the main.js, I import require.js and main.js, success:

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

then in the main.js , I try to import the extend.js:

requirejs.config({
    baseUrl: 'src'
});

require(["object/extend.js"], function (extend){

});

but it failed, it tell 404, and I saw the request is :

http://127.0.0.1:8000/object/extend.js

it seems the baseUrl doesn't work

so what's wrong with my code? How can I let the baseUrl work?

War es hilfreich?

Lösung

You should not have .js in your require. It should be:

require(["object/extend"], function (extend){

});

When RequireJS sees a dot in the module name it appears to assume that this is a full URL and does not process the baseUrl, etc. Note, however, that you can still use a dot in the module name if you've defined it as a path mapping:

require.config({
  paths: {
    'a.b': 'libraries/something/a.b'
  }
});

and then later require it:

require(['a.b'], function (ab){

});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top