RequireJS paths not work

ⅰ亾dé卋堺 提交于 2021-01-29 10:48:15

问题


I'm new to RequireJS and trying to use it. I followed an example in RequireJS docs but there is some problem. I can load the jquery but not app/shell.

Root
 |__index.html
 |__javascripts
     |__main.js
     |__libs
     |    |__jquery.js
     |    |__require.js
     |__app
         |__shell.js

index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script data-main="javascripts/main.js" src="javascripts/libs/require.js"></script>
</head>
<body>

</body>
</html>

main.js

requirejs.config({
    baseUrl:'javascripts/libs',
    paths:{
            app:'../app'
        }
    });
require(['jquery','app/shell'],function($,shell){
    if($ && shell){
        console.info('Scripts loaded');
    }
});

shell.js

define(function(){
    "use strict";
    return{
        initModule:function(){
            console.info('Module init');
        }
    }
});

Web Console Errors

"NetworkError: 404 Not Found - http://localhost:3000/javascripts/app../default.htmshell.js"


Error: Script error for: app/shell http://requirejs.org/docs/errors.html#scripterror

Node.js Express Console Error

GET /javascripts/app../default.htmshell.js 404


回答1:


Because you have multiple folder levels, you have to set your baseUrl as the root of your application. If you want to use require(['jquery']) instead of require(['libs/jquery']), you have to specify the "alias" of it in your configuration.

requirejs.config({
    baseUrl:'javascripts',
    paths:{
            jquery: 'libs/jquery'
        }
    });



回答2:


Try adding your main.js file explicitly after your requirejs script tag:

<script src="javascripts/libs/require.js"></script>
<script src="javascripts/main.js"></script>

This will load your configuration file synchronously after loading requirejs thus making sure the paths are set before a module is called. Hope it helps.



来源:https://stackoverflow.com/questions/20761867/requirejs-paths-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!