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.

可能是版本问题,升级下试试。


没看出问题但是为什么你的requirejs的require.config不是一个方法?
https://cdn.bootcss.com/requi...

>require
function requirejs()
>require.config
function req.config()

npm install require.js 安装的是1.0版本

npm install requirejs安装的是2.0+版本

版本升级应该就没问题了



相关阅读:
file:///C:/Users...这种file://开头的本地文件不能使用ajax获取数据?
angular 中ng-repeat能不能遍历指定顺序?
请问cx_oracle使用where col in (:1)的方式的时候,是否可以直接传入一个list或者tuple实现查询呢?
js 是怎么逐渐的成为万能语言的 ?
Python爬虫爬资源时由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。怎么破?
Markdown编辑器服务器处理最佳实践
html中有多个form标签,每一个form标签下对应一个submit,为什么未输入内容的form表单也会提交?
canvas背景不透明,但内部某元素透明(类似镂空效果)
php制作中英文两版网站比较方便的思路
使用iconfont在线使用时,出现多个空格,求解答
使用pjax的时候直接跳转到页面了,不能替换页面中的某个div
gdb都能调试什么类型的文件?
这种在App加载页面前的显示是什么技术或者框架?
boostrap-table可以接收string类型吗
访客能够通过猜测的方式伪造出一个正确的session id并进行不好的行为吗?
react中const {dispatch} = this.props;
tomcat配置问题
vue-router如何将接口返回的数据传给组件初始化
为什么我的程序跑的很慢?
刚入门PHP,是否有必要学习JAVA



快速导航

Copyright © 2016 phpStudy |