这是我在Require.js上的第一次尝试,我试图弄清楚如何使用jQuery插件,特别是应该管理我的SPA的history.js。下面的代码位于main.js中,并且脚本运行,但我无法计算如何实际使用History.pushState(),State.getState()和其他插件方法。我在控制台中不断收到"getState is not a function"
错误消息。require history.js插件
(function() {
requirejs.config(
{
baseUrl: 'js',
paths: {
'jquery': 'vendor/jquery-1.11.2.min',
'history': 'history.js'
},
shim: {
'history': {
deps: ["jquery"],
exports: 'History'
}
},
map: {
'*': { 'jquery': 'jquery-private' },
'jquery-private': { 'jquery': 'jquery' }
}
}
);
define(["jquery"], function($) {
$(function() {
require(['history'], function(History){
var
History = window.History,
State = History.getState();
});
});
});
})();
jQuery的私人包含此代码:
define(['jquery'], function (jq) {
return jq.noConflict(true);
});
这一切到目前为止,我已经得到了,因为我只是想弄清楚的东西出来,成立了一个文件。我对exports: History
和require(['history])
中的函数参数有一些疑问,但它可能是完全不同的。顺便说一句,我已经在之前的项目中使用过历史,所以我觉得我理解如何使用它,以防万一您可能想知道。 :)
编辑:我使用history.js文件的压缩/未压缩版本。添加一条路径并没有改变一件事。无论有没有它,我都可以在需要时看到在浏览器中加载的脚本。我仍然得到History.getState()不是一个函数错误。
谢谢。
我很惊讶你没有为'history'一个'paths'设置。他们分发的多个文件中的哪一个是您加载的?当我看[这里](https://github.com/browserstate/history.js/tree/master/scripts)时,我看到一大堆子目录中有一堆文件。另外,如果你在调用'History.getState()'之前检查'window.History',你会得到什么? – Louis
我正在使用(un)压缩的history.js文件。我用这些信息编辑了原文。如果在调用History.getState()之前检查它,我会得到一个History函数。 [链接](https://i.imgsafe.org/3f009d92d4.jpg) – skrunic