简体   繁体   English

Shim命令在RequireJS中工作

[英]Shim command working in RequireJS

I'm working with RequireJs and i'm quit new to it. 我正在与RequireJs合作,并且对它不熟悉。 I have an existing project and JavaScript code and now I'm changing that code into the Modules to be used in RequireJS. 我有一个现有的项目和JavaScript代码,现在我将该代码更改为要在RequireJS中使用的模块。 There are many third party APIs being used in my project like Bootstrap , JQuery & slickgrid etc. which are non-AMD(Asynchronous module definition). 我的项目中使用了许多第三方API,例如Bootstrap,JQuery和slickgrid等,它们都是非AMD(异步模块定义)的。 I know non-AMD Js files are required to load with shim command like 我知道需要使用shim命令加载非AMD Js文件

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

but I dont know how does shim works and its benefit. 但我不知道补片的工作原理及其好处。 Can't we simply include these js files conventional way like tag? 我们不能简单地像标记一样以常规方式包含这些js文件吗? can anyone please explain me how does it works. 谁能解释一下它是如何工作的。 Also I want to load my non-AMD files with shim command how can I load them? 我也想用shim命令加载非AMD文件,如何加载它们? My js files have dependencies on eachother so do i need to write the command 我的js文件彼此依赖,所以我需要编写命令

 deps: ['file1', 'file2']

for eachfile within shim command? shim命令中的每个文件? It would be much complex as i have over 20 JS files. 这将非常复杂,因为我有20多个JS文件。 Is there any better way to do this ? 有什么更好的方法吗? Please let me know if i'm missing something or i'm Wrong. 请让我知道我是否缺少某些东西或我错了。 I'll be very thankful for any kind support. 我将非常感谢您的支持。

It is a very Common doubt, in your case you need to have Jquery, Underscore and Backbone inside the paths configuration to use specify from where require will load the JS files. 这是一个非常普遍的疑问,在您的情况下,您需要在路径配置中包含Jquery,Underscore和Backbone,以使用从require将加载JS文件的位置指定。 Then inside the Shim block, you can specify your dependecies and how you'd like to use it in your app(exports). 然后,在Shim块中,您可以指定依赖项以及如何在应用程序(导出)中使用它。

To understand how shim works, I suggest you to read this article - http://aaronhardy.com/javascript/javascript-architecture-requirejs-dependency-management/ 要了解填充程序的工作原理,建议您阅读这篇文章-http://aaronhardy.com/javascript/javascript-architecture-requirejs-dependency-management/

Or the require JS web site. 或require JS网站。

http://requirejs.org/docs/api.html#config-shim http://requirejs.org/docs/api.html#config-shim

Hope it helps. 希望能帮助到你。

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

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