我想加載localforage與require.js庫,但不能得到它的工作。這裏是我的require.js配置:localforage與Require.js
main.html中:
<body>
<script type="text/javascript" src="/xyz/js/lib/require-2.1.22.js"> </script>
<script type="text/javascript" src="/xyz/js/main.js?version=1.0.0"></script>
</body>
main.js:
var appVersion = "1.0.0";
requirejs.config({
baseUrl: "../js",
paths: {
jquery: "lib/jquery-1.12.1",
jquery_caret: "lib/jquery.caret-1.5.1",
lodash: "lib/lodash-4.5.1",
localforage: "lib/localforage"
},
shim: {
jquery_caret: ["jquery"]
},
urlArgs: "version=" + appVersion
});
var scriptSources = ["jquery", "jquery_caret", "localforage", "lodash"];
require(scriptSources, function() {
//all scripts loaded
});
但是,當我後來TY使用localforage這樣
localforage.setDriver(localforage.INDEXEDDB).then(function() {
console.log("Hi there");
});
它總是未定義的。順便說一句。與Dexie我遇到同樣的問題。 當我從README
define(['localforage'], function(localforage) {
// As a callback:
localforage.setItem('mykey', 'myvalue', console.log);
// With a Promise:
localforage.setItem('mykey', 'myvalue').then(console.log);
});
我從requirejs.org/docs/errors.html#mismatch 收到錯誤添加以下代碼,但它是什麼意思?有人可以向我解釋嗎?我真的不明白。
與此同時,在我看來,我必須將上述定義調用從「define(['localforage'],function(localForage)」更改爲「define('localforage',['localforage'],function (localForage)「,現在呼叫不再是匿名了,但是'localforage'仍然沒有定義! –
如果其他人感興趣我已經通過使用這裏的解決方案使它工作了,即使我沒有使用縮小版本:https://github.com/mozilla/localForage/issues/58 –