【问题标题】:Require.js and Imager.js undefined functionRequire.js 和 Imager.js 未定义函数
【发布时间】:2014-02-20 06:48:26
【问题描述】:

我在将 Imager.js 库添加到 require.js 加载时遇到问题,脚本可以在没有 require.js 的情况下完美运行

Main.js

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($) {
        'use strict';
        $('.dropdown-toggle').dropdown();
        $('#bvideo').magnificPopup({
            type: 'inline'
        });
        videojs('video-cdve', {
            'controls': true,
            'autoplay': false,
            'preload': 'auto',
            'width': 'auto',
            'height': 'auto'
        });
        new Imager({
            availableWidths: {
                320: '320x240',
                640: 'large',
                1024: 'large_x2',
            }
        });
    });

我收到此错误

Uncaught ReferenceError: Imager is not defined

当我定义 Imager 时

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($, Imager) {
        'use strict';
        $('.dropdown-toggle').dropdown();
        $('#bvideo').magnificPopup({
            type: 'inline'
        });
        videojs('video-cdve', {
            'controls': true,
            'autoplay': false,
            'preload': 'auto',
            'width': 'auto',
            'height': 'auto'
        });
        new Imager({
            availableWidths: {
                320: '320x240',
                640: 'large',
                1024: 'large_x2',
            }
        });
    });

现在我收到了这个错误

Uncaught TypeError: undefined is not a function

发生了什么事?我如何使用 require 正确添加这个库??

【问题讨论】:

  • Imager 在传递参数时排在第二位,但您在 define 数组中将其导入第三位

标签: javascript requirejs undefined amd


【解决方案1】:

您的参数以与您在需求中列出它们的顺序相同的顺序绑定到模块,因此当您执行此操作时:

define([
        'jquery',
        'bootstrap',
        'imager',
        'video'
    ],
    function($, Imager) {

您将Imager 绑定到'bootstrap' 模块的值。更改顺序:

define([
        'jquery',
        'imager',
        'bootstrap',
        'video'
    ],
    function($, Imager) {

我还考虑过您是否需要 Imager 垫片,但我在他们的 code 中看到他们检测到 AMD 环境并调用 define

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多