企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## Storage 定时存储 将字符串、数组、对象、数字...存储到本地,设置存储有效时间。常用于登录后用户信息存储等... ![](https://img.kancloud.cn/46/22/4622b585059b9092c23e90522a4f4783_1259x93.png =600x) ### 代码示例 ```javascript <template> <div class="page"> <cvu-row :gutter="16"> <cvu-col span="10"><Input v-model="copyText" placeholder="请输入文本内容" clearable></Input></cvu-col> <cvu-col span="12"> <cvu-button-group> <cvu-button type="primary" @on-click="handleSetItem">设置缓存</cvu-button> <cvu-button type="success" @on-click="handleGetItem">获取缓存</cvu-button> <cvu-button type="warning" @on-click="handleRemoveItem">删除缓存</cvu-button> <cvu-button type="danger" @on-click="handleClearItem">清空缓存</cvu-button> </cvu-button-group> </cvu-col> </cvu-row> </div> </template> <script> export default { data() { return { copyText: '' } }, methods: { handleSetItem() { if(!this.copyText) { this.$Message.warning('请输入文本内容') return } this.$cvuStorage.setItem({ key: 'storageData', value: this.copyText, time: 1 * 60 }) this.$Message.success('设置存储成功') this.copyText = '' }, handleGetItem() { this.copyText = this.$cvuStorage.getItem({ key: 'storageData' }) this.$Message.success('获取存储成功') }, handleRemoveItem() { this.$cvuStorage.removeItem({ key: 'storageData' }) this.$Message.success('删除存储成功') this.copyText = '' }, handleClearItem() { this.$cvuStorage.clearItem() this.$Message.success('清空存储成功') this.copyText = '' } } } </script> <style lang="scss" scoped> .page{ height: 100%; padding: 15px; box-sizing: border-box; h3{ margin: 10px 0; } /deep/.ivu-input{ height: 40px; line-height: 40px; } } </style> ``` ### methods 设置存储 >[success] `this.$cvuStorage.setItem(options)` - options: - key:数据键名 - value:数据内容 (String/Boolean/Number/Array/Object) - time:有效时间,单位:s - type:存储工具名(localStorage[默认]/sessionStorage) 获取存储 >[success] `this.$cvuStorage.getItem(options)` - options: - key:数据键名 - type:存储工具名(localStorage[默认]/sessionStorage) 删除存储 >[success] `this.$cvuStorage.removeItem(options)` - options: - key:数据键名 - type:存储工具名(localStorage[默认]/sessionStorage) 清空存储 >[success] `this.$cvuStorage.clearItem(options)` - options: - type:存储工具名(localStorage[默认]/sessionStorage)