【问题标题】:RequireJS + Waypoints : Object [object Object] has no method 'waypoint'RequireJS + Waypoints:Object [object Object] 没有方法“waypoint”
【发布时间】:2013-10-11 14:50:24
【问题描述】:

尽管一切看起来都不错,但我不能在 RequireJS 中使用航点。 这是我的代码:http://jsfiddle.net/7nGzj/

ma​​in.js

requirejs.config({
    "baseUrl": "theme/PereOlive/js/lib",
    "paths": {
      "app":"../app",
      "jquery": "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min"
    },
    "shim": {
        "waypoints.min": ["jquery"]
    }
});
requirejs(["app/common"]);

common.js

define([
    'jquery',
    "waypoints.min",
], function () {
    $(function () {
        console.log($.waypoints);
        $('#loading').waypoint(function (direction) {
            // do stuff
        });
    });
});

我什至调整了它以确保 jQuery 已正确加载,但它不起作用。 我的其他库按应有的方式工作(responsiveslides、flexslider、hoverintent、smoothscroll 等)。

  • jQuery V1.10.2
  • 航点 V2.0.3
  • RequireJS V2.1.8

【问题讨论】:

    标签: jquery requirejs jquery-waypoints


    【解决方案1】:

    符合 AMD 标准(Waypoints 也是)的依赖项应该是 required,通过它们注册的模块名称。找出该名称的最简单(唯一?)方法是查看 lib 的源代码:

    // (...)
    if (typeof define === 'function' && define.amd) {
      return define('waypoints', ['jquery'], function($) {
        return factory($, root);
      });
    } // (...)
    

    它是“waypoints”!

    由于该库与 AMD 兼容,因此您不需要 shim,但您需要定义它的路径(因为文件名可以不同于 AMD 模块名):

    requirejs.config({
        "baseUrl": "theme/PereOlive/js/lib",
        "paths": {
          "app": "../app",
          "waypoints": "path/to/waypoints.min"
        }
    });
    

    完成此操作后,您需要更改您的require 电话:

    define([
        "jquery",
        "waypoints" // this is the AMD module name
    ]
    

    修复了你的 jsfiddle:http://jsfiddle.net/jVdSk/2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 2015-06-09
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2015-05-08
      • 1970-01-01
      相关资源
      最近更新 更多