本文介绍了使用require.js加载spin.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始了一个Javascript项目,现在我将它移到require.js。到目前为止,除了库之外,一切正常。当我尝试访问任何与spin.js相关的内容时,我收到以下错误消息:

I recently started a Javascript project and I am now moving it to require.js. Everything worked fine so far except for the spin.js library. I get the following error message when I try to access anything spin.js related:

我的 requirejs.config 如下所示:

requirejs.config({

    baseUrl: 'js',

        paths: {
            'jquery': 'lib/jquery',
            'spin': 'lib/spin',
    },

    shim: {
        'jquery' : {
            deps: [],
        },

        'spin' : {
            deps: [],
            exports: 'Spinner'
        },
    }
});

示例模块如下所示:

require(['spin'],
    function(Spinner)
    {
        new Spinner();
    }
);

我正在使用 shim config 因为我有一些其他依赖的模块。其他一切似乎都很好。我在这里缺少什么?

I'm using the shim config because I have some other modules with dependencies. Everything else seems to be loading fine though. What am I missing here?

Alex指出我的图书馆包含错误。对于每个在理解backbone.js和require.js时遇到麻烦的人,我建议,尤其是章节关于模块化开发。

As Alex pointed out my library inclusion was wrong. For everyone having troubles with understanding backbone.js and require.js I suggest this book, especially the chapter about modular development.

推荐答案

旋转库不应该在配置中填充。来自spin.js源代码:

The spin library should not be shimmed in your config. From the spin.js source code:

  if (typeof define == 'function' && define.amd)
    define(function() { return Spinner })
  else
    window.Spinner = Spinner

它已经在最后被定义为一个模块,而window.Spinner不是作为一个窗口对象创建的(这就是为什么它不应该是shimmed)

It is already defined as a module here at the end, and window.Spinner is not created as a window object (which is why it shouldn't be shimmed)

这篇关于使用require.js加载spin.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:57