requirejs中shim使用


对不符合AMD规范的js脚本使用 shim 做导出配置,哪里不对?
demo: https://github.com/xiaodongzai/demos/tree/gh-pages/requirejs_shim

clipboard.png


 <!-- index.html -->
<!DOCTYPE html>
<html>
<head lang="zh">
    <meta charset="UTF-8">
    <title>shim的使用</title>
</head>
<body>

<script src="./js/require.js" data-main="./js/app"></script>
</body>
</html>


 //app.js
require.config({
    baseUrl: "./js",
    paths:{
        'jquery': "jquery",
        'log': 'log'
    },
    shim:{
        'log':{
            deps: ['jquery'],
            exports: 'log'
        }
    }
});

require(['jquery','log'], function ($,log) {
    log.writeLog('测试测试!!!');
});


 //log.js
function writeLog(x){
    document.write(x);
}

异常信息:

clipboard.png

require web前端开发 JavaScript

lu——舞 8 years, 11 months ago

你的log模块没有定义,所以不能require。
你的log.js里面应该这样写:

define(function(){

var log = {};
log.writeLog = function(str){


 document.write(str);

};
return log;
});

栐恆D噓箜 answered 8 years, 11 months ago


 require(['jquery','log'], function ($,log) {
    console.log(log);
});

打印出来是什么?

怪叔叔@——@ answered 8 years, 11 months ago

Your Answer