localStorage (string)
localStorage.setItem(key, value)
localStorage.getItem(key)
localStorage.removeItem(key)
localStorage.clear()
sessionStorage (string) 关闭窗口后失效
sessionStorage.setItem(key, value)
sessionStorage.getItem(key)
sessionStorage.removeItem(key)
sessionStorage.clear()
indexedDb
注意回调函数中 this 指向
let request = window.indexedDB.open('testdb', 1)
request.onsuccess = e => {
this.db = event.target.result
}
request.onupgradeneeded = event => {
this.db = event.target.result
if (!this.db.objectStoreNames.contains('person')) {
var objectStore = this.db.createObjectStore('person', { keyPath: 'id' })
objectStore.createIndex('id', 'id', { unique: true })
}
}
request.onerror = function (event) {
}
let task = this.db.transaction(['person'], 'readwrite').objectStore('person').add({ id: 7, name: 'zhao6', age: 114, email: 'zhadan222gsan@exale.com' })
task.onsuccess = function (event) {
}
task.onerror = function (event) {
}
let task = this.db.transaction(['person'], 'readwrite').objectStore('person').index('索引name').get('索引key')
var req = indexedDB.deleteDatabase(databaseName)
service worker
import { register } from 'register-service-worker'
register('/sw.js', { registrationOptions: { scope: './' } })
var self = this
self.addEventListener('install', event => {
event.waitUntil(
caches.open('static-v1').then(cache => {
cache.addAll([
])
console.log('service worker 缓存成功!')
}),
)
})
self.addEventListener('activate', event => {
console.log('Version now ready to handle')
})
self.addEventListener('fetch', event => {
console.log('fetch', event.request)
})