简体   繁体   English

使用Require JS加载js文件

[英]Loading js file using Require JS

What is correct way to load modules through require js ? 通过require js加载模块的正确方法是什么?
I am new to Require js, and I am using it for loading different js modules required for my app. 我是Require js的新手,并且正在使用它来加载我的应用程序所需的不同js模块。 I am using Embedded JS ( EJS ) for template. 我正在使用Embedded JS( EJS )作为模板。 Below is my code: 下面是我的代码:

require.config({
                baseUrl: _URL+"/resources/",
                paths:
                {
                    moment: "js/moment.min",
                    ejs :  "js/ejs",
                    social_login : "public/social_login",
                    location : "js/location"
                },
                shim:
                {
                    ejs:
                    {
                        exports: 'ejs',
                        deps: ['moment']
                    },
                    social_login:
                    {
                         exports: 'social_login',
                         deps: ['moment','ejs']
                    },
                    location:
                    {
                         exports: 'location',
                         deps: ['moment','ejs','social_login']
                     }
                 },
                 waitSeconds: 20
            });
            require(["moment","ejs","social_login","location"],function()
            {
                callback("OK");
            });  

Some time all modules load correctly and running, but when I repeatedly press Ctrl + F5 or F5 it gives me script error for ejs and other modules as in firebug console it gives: 有时所有模块都可以正确加载并运行,但是当我反复按Ctrl + F5F5时,它为我提供了ejs和其他模块的脚本错误,就像在Firebug控制台中那样:

在此处输入图片说明

I went through this link. 我通过此链接。 link1 which says there is a JavaScript syntax error or other execution problem running the script. link1表示运行脚本时存在JavaScript语法错误或其他执行问题。
What is the correct way to load ejs or any module ? 加载ejs或任何模块的正确方法是什么? I have downloaded latest version of it, is there any thing related with AMD module which EJS is not following ? 我已经下载了最新版本,是否有EJS不关注的与AMD模块相关的东西? I am quite blank, kindly help. 我很空白,请帮忙。 Thanks 谢谢

Load timeouts mean that the scripts didn't load. 加载超时意味着脚本没有加载。 Check for 404s on the Network tab and look at the request paths if they are correct. 在“网络”选项卡上检查404,然后查看请求路径(如果正确)。 Adjust your paths accordingly. 相应地调整路径。

Also, don't use minified versions of libraries. 另外,请勿使用精简版的库。 This makes them hard to debug. 这使它们难以调试。 RequireJS has its own optimizer which you can use when you push for production after development. RequireJS拥有自己的优化器,您可以在开发后将其用于生产时使用。 It will compress all required files into one file. 它将所有需要的文件压缩为一个文件。

In addition, you should use front-end package managers like Bower . 另外,您应该使用诸如Bower之类的前端软件包管理器。 That way, you won't be copy-pasting libraries manually and prevent syntax errors caused by incorrect copy-paste. 这样,您就不会手动复制粘贴库,并且可以防止由于错误的复制粘贴而导致语法错误。 I personally use Webapp Generator to scaffold the app for me, so that I won't be copy-pasting libraries. 我个人使用Webapp Generator来为我搭建应用程序,这样我就不会复制粘贴库了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM