用于封装和加载全局可以用的枚举模块

利用枚举,维护全局使用的错误信息吗,还有错误信息标志,该方法与封装上送给前端的快递包是结合使用
public enum ResponseCode {
    //基本类型参数
  SUCCESS(0,"SUCCESS"),
    ERROR(1,"ERROR"),
    NEED_LOGIN(10,"NEED_LOGIN"),
    ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");

    //成员变量
    private final int code;
    private final String desc;

    //构造方法
    ResponseCode(int code,String desc){
        this.code=code;
        this.desc=desc;
    }

    //外抛开发
    public int getCode(){
        return code;
    }
    public String getDesc(){
        return desc;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
存储操作类? 要封装一个可以全局使用的localStorage存储操作类,可以按照以下步骤进行: 1. 创建一个Storage类,用来封装localStorage的读写操作方法。 2. 通过ES6的class方式定义Storage类,可以包含如下方法: - set(key: string, value: any): void //存储某个键值对 - get(key: string): any //获取某个键对应的值 - remove(key: string): void //移除某个键值对 - clear(): void //清除所有的键值对 3. 在Storage类中,可以通过判断浏览器是否支持localStorage来决定使用哪种存储方式,如localStorage或cookie等。 4. 定义一个全局变量,用来存储Storage实例,可以在需要使用localStorage的时候,直接调用全局变量中的Storage实例即可。 5. 最后,在页面加载的时候,初始化全局变量中的Storage实例,并向全局下挂载Storage对象,以便在任何地方都可以使用。 示例代码如下: ```javascript class Storage { constructor() { this.store = window.localStorage || null; } set(key, value) { if (this.store) { this.store.setItem(key, JSON.stringify(value)); } } get(key) { if (this.store) { const value = this.store.getItem(key); return JSON.parse(value); } } remove(key) { if (this.store) { this.store.removeItem(key); } } clear() { if (this.store) { this.store.clear(); } } } const storage = new Storage(); window.Storage = storage; ``` 注:上述代码中,使用JSON.stringify()和JSON.parse()方法将值转为JSON字符串和对象,以便能够储存和读取对象类型的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值