requirejs 关于shim的使用方式
一直在搞移动开发,很久没写过有关javaScript的文章了,最近公司开发了自己的web框架,用到了requirejs,之前用过,借此复习一下,特别是依赖问题。 requirejs具有以下是异步加载,当然已经加载的不会再次加载,这是非常好的一种优化。当然,我们这里来看看shim的作用。 导出非AMD模块化函数(模块化) hello.js function sayHello(name) { alert('Hi '+name); } requirejs.config({ baseUrl: '/public/js', paths: { hello: 'hello' //相对于baseUrl的方式导入hello.js ,并且给一个别名 }, shim: { hello: { exports: 'sayHello' } //因为是一个函数,使用export即可 } }); requirejs(['hello'], function(hello) { //function参数接收导出的函数名,实际是sayHello函数 hello(); }); 导出一个函数,意味着什么?意味着我们得到了一个javaScript类,所以已经满足了绝大多数需求。 但是有一个问题,在app.js中写了很多function,整合成一个function有点费劲,想直接导出如何做? 办法如下: hello.js