项目作者: MianSaleem

项目描述 :
A Vue plugin for localForage
高级语言: JavaScript
项目地址: git://github.com/MianSaleem/vue-forage.git
创建时间: 2018-03-19T04:06:19Z
项目社区:https://github.com/MianSaleem/vue-forage

开源协议:MIT License

下载


vue-forage

A Vue.js wrapper plugin for localForage

Installation

Simply install the npm package vue-forage:

NPM

  1. npm i vue-forage

YARN

  1. yarn add vue-forage

Using vue-forage

In your main JavaScript file (eg. main.js or app.js):

  1. // Import Vue and vue-forage
  2. import Vue from 'vue';
  3. import vf from 'vue-forage';
  4. // Tell Vue.js to use vue-forage
  5. Vue.use(vf);

In your app/components:

  1. // configure your local storage
  2. this.$vf.config({
  3. name: 'vue-forage'
  4. });
  5. // change driver
  6. // this.$vf.useWebSQLDriver();
  7. // this.$vf.useIndexedDBDriver();
  8. this.$vf.useLocalStorageDriver();
  9. // this.$vf.setDriver(YOURDRIVER);
  10. // SET ITEM
  11. // this.$vf.setItem('key', 'value');
  12. this.$vf.setItem('app', 'Vue Forage');
  13. // or
  14. this.$vf.setItem('app', { app: 'Vue Forage', version: '1.0.0', author: { name: 'John Doe', email: 'john.doe@mail.com' }});
  15. // GET ITEM
  16. // this.$vf.getItem('key'');
  17. this.$vf.getItem('app');
  18. // REMOVE ITEM
  19. // this.$vf.removeItem('key'');
  20. this.$vf.removeItem('app');
  21. this.$vf.clear(); // delete everything
  22. // Forage will stringify/parse the json object automatically.

All the methods will return promise. Use .then() and .catch() whereever you need.

* Please refer to localForage documentation for more info, you can view the localForage Docs at https://localforage.github.io/localForage

Default Driver

By default, localForage selects backend drivers for the datastore in this order:

  1. IndexedDB localforage.INDEXEDDB
  2. WebSQL localforage.WEBSQL
  3. localStorage localforage.LOCALSTORAGE

If you would like to force usage of a particular driver you can this.$vf.useLocalStorageDriver(); or this.$vf.useIndexedDBDriver(); or this.$vf.useWebSQLDriver(); or for custom drivers this.$vf.setDriver(yourdriver);

  1. this.$vf.createInstance({
  2. storeName: 'user' // name of the datastore for IndexedDB & WebSQL - must be alphanumeric, with underscores
  3. }).then((store) => {
  4. store.setItem('key', ['some', 'value']);
  5. store.length().then((keys) => {
  6. console.log(keys);
  7. });
  8. store.iterate((value, key, num) => {
  9. return [key, value];
  10. }).then((result) => {
  11. console.log(result);
  12. });
  13. });

API will work same as localForage in your vue app with this.$vf, only the json has been added to update json objects easily.

You can use . notation for json object, if you need to update the version in above app code then you can simple

  1. this.$vf.json('app.version', '1.0.1');

and to update author name

  1. this.$vf.json('app.author.name', 'Mian Saleem');

json will resolve with main object, in above example code .then(value => console.log(value)) will log the updated app object.

Contributing

Any sort of contributions and feedback is much appreciated. Just leave an issue or pull-request!