javascript - RequireJS QCodeDecoder 导出未捕获的类型错误 : object is not a function

标签 javascript django requirejs

我正在尝试使用 qcode-decoder 作为库之一 ( https://github.com/cirocosta/qcode-decoder/blob/master/src/qcode-decoder.js ) 在我的 django 项目中实现 requirejs。 在 html 中我这样做:

<script src="{{STATIC_URL}}js/libs/require.js"></script>
<script src="{{STATIC_URL}}js/pages/pay_iframe.js"></script>

这是我的 pay_iframe.js:

requirejs.config({
    baseUrl: '/static/js/pages',
    paths: {
        qcode_decoder: '../libs/qcode/qcode-decoder',
        porthole: '../libs/porthole',
        jquery: '../libs/jquery',
        pay_iframe: 'pay_iframe',
    },
    shim: {
        'qcode_decoder': {
            exports: 'QCodeDecoder',
        },
        'pay_iframe': {
            deps: ['jquery', 'qcode_decoder', 'porthole'],
        },
    }
});

require(['jquery', 'qcode_decoder', 'porthole'], function($, QCodeDecoder) {
    console.log(QCodeDecoder);
    var qr = new QCodeDecoder();
    if (!(qr.isCanvasSupported() && qr.hasGetUserMedia())) {
        ...
    }
    ...
});

一切似乎都以正确的顺序加载正常,但是当我尝试创建 QCodeDecoder 对象(顺便说一句,它在实现 requirejs 之前工作)时,我收到以下错误:

Uncaught TypeError: object is not a function

我猜这是因为 QCodeDecoder 是一个对象(由 console.log 打印):

Object {imagedata: null, width: 0, height: 0, qrCodeSymbol: null, debug: false…}

但是这个对象不包含以后使用的函数(isCanvasSupported 和 hasGetUserMedia)所以我不能这样做:

var qr = QCodeDecoder;

也不行。 也许我应该导出(在垫片配置中)其他东西? 请帮帮我。

最佳答案

require 更改为 define 即可解决。

关于javascript - RequireJS QCodeDecoder 导出未捕获的类型错误 : object is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28297862/

相关文章:

javascript - 重新设计具有固定大小的站点

javascript - 如何处理 REST 中的非典型操作?

django sekizai {% addtoblock %} 标签工作不正常

python - 用于具有多个应用程序的现有 Django 项目的 herokusyncdb

javascript - RequireJS 中依赖关系的相关路径定义

javascript - 对于多个页面,使用单个 requireJs 配置还是多个?

javascript - Javascript 中的 += 是什么?

javascript - 使用与 HtmlWebpackPlugin 和 EJS 相同的模板文件?

python - 在 Django admin 中对内联进行单元测试验证

javascript - AngularJS的依赖注入(inject)和RequireJS本质上是一样的吗?