多实例

优质
小牛编辑
123浏览
2023-12-01

你可以创建多个 localForage 实例,且能指向不同数据仓库。所有 config 中的配置选项都可用。

createInstance

var store = localforage.createInstance({
  name: "nameHere"
});

var otherStore = localforage.createInstance({
  name: "otherName"
});

// 设置某个数据仓库 key 的值不会影响到另一个数据仓库
store.setItem("key", "value");
otherStore.setItem("key", "value2");

创建并返回一个 localForage 的新实例。每个实例对象都有独立的数据库,而不会影响到其他实例。

dropInstance

localforage.dropInstance().then(function() {
  console.log('Dropped the store of the current instance').
});

localforage.dropInstance({
  name: "otherName",
  storeName: "otherStore"
}).then(function() {
  console.log('Dropped otherStore').
});

localforage.dropInstance({
  name: "otherName"
}).then(function() {
  console.log('Dropped otherName database').
});

调用时,若不传参,将删除当前实例的 “数据仓库” 。

调用时,若参数为一个指定了 namestoreName 属性的对象,会删除指定的 “数据仓库”。

调用时,若参数为一个仅指定了 name 属性的对象,将删除指定的 “数据库”(及其所有数据仓库)。