RequireJS中如果使用AMD规范,在使用的过程中没有太多的问题,如果加载非AMD规范的JS文件,就需要使用Require中的shim.
require.config({ ???paths:{ ???????jquery:"/js/jquery2.0", ???????InStorage:"/js/in/InStorage", ???????Product:"/js/product/Product", ???????cate:"/js/product/Category", ???}, ???shim:{ ???????cate:{ ???????????deps:[], ???????????exports:"Category" ???????} ???}});
cate:"/js/product/Category" 该文件是非AMD规范的JS,在使用的过程中遵循如下几个步骤:
(1) paths 中配置文件加载的路径, JSON中的 Key值可以随意,尽量有意义,JSON中的Value是文件的加载路径,这个不必多说
(2) shim 中定义一个JSON对象, Key 值(cate) 与paths中定义的名字一样
(3) shim中的JSON对象有两个属性: deps,exports ; deps 为数组,表示其依赖的库, exports 表示输出的对象名
var Category=(function(){ ???var param={}; ???param.Add=function(){ ???????console.log("新增分类"); ???} ???return param;})();var Category=(function(param){ ???param.Write=function(){ ???????console.log("输出分类信息"); ???} ???return param;})(Category||{});
requirejs可以实现js的延时加载, 在方法调用的时候加载JS,也就是在function 中require 某个模块的信息
define(function(){ ???var ProductManager={ ???????Create:function(){ ???????????console.log("创建产品"); ???????????require(["cate"],function(cate){ ???????????????cate.Write(); ???????????????cate.Add(); ???????????}); ???????} ???} ???return ProductManager;});
(转)RequireJS shim 用法说明
原文地址:https://www.cnblogs.com/ximenxiazi/p/9055469.html