1 npm
npm install idb
2 代码
<template><div><p>Data: {{ data }}</p><button @click="fetchData">Fetch Data</button></div>
</template><script>
import { openDB } from 'idb';export default {data() {return {data: null}},methods: {async fetchData() {// 发送第三方请求 url 填写const response = await fetch(url);const data = await response.json();// 将数据存储到 IndexedDB 中let db = await openDB('my-store', 1, {upgrade(db) {db.createObjectStore('my-store');}});db = await openDB('my-store', 1);)const tx = db.transaction('my-store', 'readwrite');const store = tx.objectStore('my-store');console.log('store', store)await store.put(data, 'my-data');await tx.done;// 从 IndexedDB 中获取数据const tx2 = db.transaction('my-store');const data2 = await tx2.objectStore('my-store').get('my-data');await tx2.done;// 更新数据this.data = data2;console.log(data2)}}
}
</script>