require.config is not a function
用webpack是正常,但由于某些原因,还是试了下requirejs,但结果出错了,暂时找不出原因。
main.js
require.config({
paths: {
"Vue": "javascripts/vue",
"VueRouter": "javascripts/vue-router"
}
});
require(['Vue', 'VueRouter'], function (Vue, VueRouter) {
// 具体代码
});
页面加载代码
<script src="../javascripts/require.min.js" data-main="../main"></script>
看不出哪里错了,可是结果错了。
如果这样写会报错
<script src="../javascripts/require.min.js"></script>
<script src="../main.js"></script>
报错
Uncaught TypeError: require.config is not a function
写了个测试。
<script src="../javascripts/require.min.js"></script>
<script src="../javascripts/test.js"></script>
test.js执行正常。
test.js
define('module', [], function(dep1, dep2) {
console.log('setup module1');
return {
api: function() {
console.log('from module1');
}
};
});
define('module2', [], function() {
console.log('setup module2');
var module = require('module');
return {
api2: function() {
console.log('from module2');
}
};
});
define('module3', ['module'], function(module) {
console.log('setup module3');
return {
api3: function() {
console.log('from module3');
}
};
});
define('main', ['module2', 'module'], function(module2, module) {
module2.api2();
module.api();
require('module3').api3();
});
console.time('time');
require('main');
console.timeEnd('time');
<script data-main="../javascripts/test" src="../javascripts/require.min.js"></script>
不执行,暂时还没弄清楚。
可能原因
If you get that error, it could be that:
RequireJS is not loaded.
This can diagnosed by opening the browser's debugger to look at network requests and seeing whether the file that defines require is asked by the browser and checking that the response indicates that the data is available (i.e. loaded from the server, loaded from cache).
RequireJS is loaded but not initializing properly.
This could happen if something is loaded before RequireJS and somehow messes with the JavaScript runtime in a way that makes RequireJS fail to initialize. For instance, if something puts in the global space a global named define of any type, or global named requirejs and which is a function, then RequireJS will silently abandon initializing.
I'd diagnose this with breakpoints in the file that contains RequireJS to see whether it completes execution or not.
RequireJS is loaded but something else redefines require.
I'd inspect the value of require just after RequireJS is loaded. If it has a config method defined at that point, then something else must be messing with it later.
初步验证:
RequireJS初始化和加载没问题;
写了个简单的例子测试,应该没有其他引用导致RequireJS初始化失败;
RequireJS版本1.0.
可能是版本问题,升级下试试。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
npm install require.js 安装的是1.0版本
npm install requirejs安装的是2.0+版本
版本升级应该就没问题了
没看出问题但是为什么你的requirejs的require.config不是一个方法?
https://cdn.bootcss.com/requi...