2016-03-03 86 views
1

我想加载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 收到错误添加以下代码,但它是什么意思?有人可以向我解释吗?我真的不明白。

+0

与此同时,在我看来,我必须将上述定义调用从“define(['localforage'],function(localForage)”更改为“define('localforage',['localforage'],function (localForage)“,现在呼叫不再是匿名了,但是'localforage'仍然没有定义! –

+0

如果其他人感兴趣我已经通过使用这里的解决方案使它工作了,即使我没有使用缩小版本:https://github.com/mozilla/localForage/issues/58 –

回答

-1

您可以试用回购示例,特别是this one。 在更多的细节完整main.js文件应该是这样的:

requirejs.config({ 
 
    paths: { 
 
     localforage: 'path/to/dist/localforage' 
 
    } 
 
}); 
 
define(['localforage'], function(lf) { 
 
    lf.ready().then(function() { 
 
     var key = 'STORE_KEY'; 
 
     var value = 'What we save offline'; 
 

 
     lf.setItem(key, value).then(function() { 
 
      console.log('SAVING', value); 
 

 
      return lf.getItem(key); 
 
     }).then(function(readValue) { 
 
      console.log('READING', readValue); 
 
     }); 
 
    }); 
 
});

此外,getItems插件有an example也似乎与你相似。

+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面发生变化,则无效 - [来自评论](/ review/low-quality-posts/17793815) – AK47