NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
[TOC] > [参考](https://wangdoc.com/javascript/bom/storage.html#storagegetitem) ## 概述 localStorage生命周期是永久的,除非被清除,否则永久保存 而sessionStorage仅在当前会话下有效,关闭页面或浏览器后被清除 ### 属性与方法 ``` interface Storage { readonly length: number; clear(): void; getItem(key: string): string | null; key(index: number): string | null; removeItem(key: string): void; setItem(key: string, value: string): void; [name: string]: any; } ``` ## 实例 ### 三种写法 ``` // 下面三种写法等价 window.localStorage.foo = '123'; window.localStorage['foo'] = '123'; window.localStorage.setItem('foo', '123'); ···