zoukankan      html  css  js  c++  java
  • storage封装

    storage.js

     1 /*
     2     storage 主要放项目中的storage相关操作:存取等
     3 */
     4 var storage = {
     5     /**
     6      对本地数据进行操作的相关方法,如localStorage,sessionStorage的封装
     7     */
     8     setStorage: function(key, value, duration) {
     9         var data = {
    10             value: value,
    11             expiryTime: !duration || isNaN(duration) ? 0 : this.getCurrentTimeStamp() + parseInt(duration)
    12         };
    13         localStorage[key] = JSON.stringify(data);
    14     },
    15     getStorage: function(key) {
    16         var data = localStorage[key];
    17         if (!data || data === "null") {
    18             return null;
    19         }
    20         var now = this.getCurrentTimeStamp();
    21         var obj;
    22         try {
    23             obj = JSON.parse(data);
    24         } catch (e) {
    25             return null;
    26         }
    27         if (obj.expiryTime === 0 || obj.expiryTime > now) {
    28             return obj.value;
    29         }
    30         return null;
    31     },
    32     removeStorage: function(key){
    33         localStorage.removeItem(key);
    34     },
    35     getSession: function(key) {
    36         var data = sessionStorage[key];
    37         if (!data || data === "null") {
    38             return null;
    39         }
    40         return JSON.parse(data).value;
    41 
    42     },
    43     setSession: function(key, value) {
    44         var data = {
    45             value: value
    46         }
    47         sessionStorage[key] = JSON.stringify(data);
    48     },
    49     getCurrentTimeStamp: function() {
    50         return Date.parse(new Date());
    51     }
    52 };
    53 export default storage;

    在main.js里面引用

    import storage from '@/utils/storage.js'
    Vue.config.productionTip = false

    //将常用工具方法扩展成Vue实例
    Vue.prototype.$storage=storage;

    日常所遇,随手而记。
  • 相关阅读:
    Docker之概述
    redis命令
    spring mvc(1) 为什么要使用mvc
    学习到的
    HttpWebRequest简单使用
    推手总结
    react 生命周期
    利用反射对应数据库字段
    扩展方法
    发送请求并返回
  • 原文地址:https://www.cnblogs.com/zhihou/p/8041943.html
Copyright © 2011-2022 走看看