InDB是一个npm包,通过npm进行安装:
npm i indb
安装之后,你可以根据你的编程需求选择不同的使用方式:
// webpack import InDB from ‘indb‘
// CommonJS
const { InDB } = require(‘indb‘)
在HTML中直接使用时,你需要把indb/dist/indb.js文件拷贝到你的本地目录:
<script src="./node_modules/indb/dist/indb.js"></script>
<script>
const { InDB } = window[‘indb‘] // 注意这里的使用方法
</script>
在进行InDB实例化时,需要传入参数:
const { InDB } = window[‘indb‘]
const idb = new InDB({
name: ‘mydb‘,
version: 1,
stores: [
{
name: ‘store1‘,
keyPath: ‘id‘,
},
{
name: ‘store2‘,
isKv: true,
},
],
})
const store1 = idb.use(‘store1‘)
const store2 = idb.use(‘store2‘)
;(async function() {
// put and get data
await store1.put({ id: ‘key1‘, value: ‘value2‘ })
await store1.put({ id: ‘key2‘, value: ‘111111111‘ })
const obj = await store1.get(‘key1‘)
// use like key-value storage (as localStorage do)
await store2.setItem(‘key‘, ‘value‘)
console.log(obj)
console.log(await store2.get(‘key‘))
getkey=await store1.get(‘key2‘)
alert(getkey.id)
alert(getkey.value)
console.dir(await store1.get(‘key2‘))
})()
原文:https://www.cnblogs.com/fslnet/p/14378861.html