热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在RequireJS中以同步顺序加载脚本-LoadscriptsinasynchronousorderinRequireJS

<head><scriptlanguagejavascriptsrcrequire.jsdata-mainscriptsmain>
   
   
     
  

The main.js has all the necessary libraries required by app.js(angular module) and hence I want to ensure app.js gets loaded only after all the libraries in the main.js are resolved through require call. But since require js behaves asynchronous way, app.js file gets loaded before the libraries in main.js file is loaded and hence error resolving app.js.

main.js具有app.js(angular模块)所需的所有必需库,因此我想确保只有在main.js中的所有库通过require调用解析后才能加载app.js。但是由于要求js以异步方式运行,app.js文件在加载main.js文件中的库之前加载,因此错误解析了app.js.

Kindly pour in your suggestions to restrict app.js from loading before main.js is loaded completely.

在完全加载main.js之前,请注意您的建议以限制app.js加载。

Thanks Santhosh

1 个解决方案

#1


0  

If you write script like this in head tag, its not loaded synchronously in browser. main.js took long time to load as compare to app.js in browser. Hence you getting this error. Try to load app.js via main.js file

如果在head标签中编写这样的脚本,则不会在浏览器中同步加载。与浏览器中的app.js相比,main.js需要很长时间才能加载。因此,您收到此错误。尝试通过main.js文件加载app.js.

Your have to use shim : {} to define dependencies in requireJS config file.

您必须使用shim:{}来定义requireJS配置文件中的依赖项。

main.js

require.config({

paths: {
    'angular': '../lib/angular/angular',
    'angular-route': '../lib/angular-route/angular-route',
    'domReady': '../lib/requirejs-domready/domReady'
},

/**
 * for libs that either do not support AMD out of the box, or
 * require some fine tuning to dependency mgt'
 */
shim: {
    'angular': {
        exports: 'angular'
    },
    'angular-route': {
        deps: ['angular']
    }
},

deps: [
    // kick start application... see bootstrap.js
    './bootstrap'
]
});

In main.js you have to bootstrap your angular App and then have to call app.js from bootstrap.js

在main.js中,您必须引导角度应用程序,然后必须从bootstrap.js调用app.js.

bootstrap.js

define([
    'require',
    'angular',
    'app',
    'routes'
], function (require, ng) {
'use strict';

/*
 * place operations that need to initialize prior to app start here
 * using the `run` function on the top-level module
 */

require(['domReady!'], function (document) {
    ng.bootstrap(document, ['app']);
    });

});

here in bootstrap file your app.js is called so always you app.js is called after main.js

在bootstrap文件中你的app.js被调用,所以你总是在main.js之后调用app.js

For more information about angular with requireJS please refer startersquad example.

有关使用requireJS设置角度的更多信息,请参阅startersquad示例。

Read this documentation for more information.

阅读本文档以获取更多信息。


推荐阅读
author-avatar
欧泊王_121
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有