15
  • What is a decorator
  • How to encapsulate the decorator
  • What can decorators do

1. What is a decorator

See an example to understand eg:
正常开发是这样的:
    1、先定义节流方法:
        methods: {
          throttle (func, delay) {            
              var timer = null;            
              return function() {                
                  var context = this;               
                  var args = arguments;                
                  if (!timer) {                    
                      timer = setTimeout(function() {             
                          func.apply(context, args);               
                          timer = null;                    
                      }, delay);                
                  }            
              }        
          }    
        }
        2、然后执行A方法:
        methods: {
             a() {
                this.throttle(()=>{
                 //执行业务逻辑
                }, 400)
            }
        }
Anyway, it is all kinds of nesting, it seems that the code is very abscessed, let's take a look at how to write [Decorator] ↓
//使用装饰器过后的写法
import { throttle} from "@/utils/decorator";

methods: {
  @throttle(400)  // 装饰器(节流)  
  a() {
    // 执行业务逻辑
    //此时会发现点击效果跟上面写法一样
    console.log('执行业务')
  },
}
Is the writing now seen a lot cooler, there is no multi-level nesting

2. How to package the decorator

    1、在工具文件创建decorator.js
    // utils/decorator.js
    /**
     * 节流,一定时间内,只能触发一次操作
     * @export
     * @param {Function} fn - 运行函数
     * @param {Number} wait - 延迟时间
     * @returns
     */
    export function throttle(wait = 2000) {
      //返回值:被传递给函数的对象。    
      return function(target, name, descriptor) {
        // @param target 类本身
        // @param name 装饰的属性(方法)名称
        // @param descriptor 属性(方法)的描述对象
        const fn = descriptor.value 
        let canRun = true
        descriptor.value = async function(...args) {
          //具体的装饰器业务在这里面编写    
          if (!canRun) return
          await fn.apply(this, args) // 执行业务下的方法
          canRun = false
          setTimeout(() => {
            canRun = true
          }, wait)
        }
      }
    }
    2、在业务板块里面声明使用
       methods: {
          @throttle(400)  // 装饰器(节流)  
          a() {
            // 执行业务逻辑
            //此时会发现点击效果跟上面写法一样
            console.log('执行业务')
          },
        }
    //现在看到代码是不是就没有那么脓肿了,就一行指令

3. What can decorators do

In real development, we often encounter throttling, anti-shake, log, button permissions, and other interception operations before business execution

The following are some decorators I usually use, I hope it will be helpful to you who see here!
// utils/decorator.js
import { Dialog } from 'vant';

/**
 * loading 开关装饰器
 * @param {String} loading 当前页面控制开关的变量名字
 * @param {Function} errorCb 请求异常的回调 返回error 一般不用写
 * 如果 errorCb 为 function 为你绑定 this  如果是箭头函数 则第二个参数为this
 * @example
 * @loading('loading',function(){})
 * async getTab(){
 *  this.tab =  this.$apis.demo()
 * }
 */
export function loading (loading, errorCb = Function.prototype) {
  return function (target, name, descriptor) {
    const oldFn = descriptor.value;
    descriptor.value = async function (...args) {
      try {
        this[loading] = true;
        await oldFn.apply(this, args);
      } catch (error) {
        errorCb.call(this, error, this);
      } finally {
        this[loading] = false;
      }
    };
  };
}

/**
 * 日志注入
 * @export
 * @param {Function} fn - 运行函数
 * @param {data} 日志需要的参数
 * @returns
 */
export function log(data) {
  return function(target, name, descriptor) {
    const fn = descriptor.value;
    descriptor.value = async function(...args) {
      await fn.apply(this, args);
      logApi(data) // 自己的日志接口
    }
  }
}

// utils/decorator.js
/**
 * 节流,一定时间内,只能触发一次操作
 * @export
 * @param {Function} fn - 运行函数
 * @param {Number} wait - 延迟时间
 * @returns
 */
export function throttle(wait= 2000) {
  return function(target, name, descriptor) {
    const fn = descriptor.value
    let canRun = true
    descriptor.value = async function(...args) {
      if (!canRun) return
      await fn.apply(this, args)
      canRun = false
      setTimeout(() => {
        canRun = true
      }, wait)
    }
  }
}
// utils/decorator.js
/**
 * 防抖,连续操作时,只在最后一次触发
 * @export
 * @param {Function} fun - 运行函数
 * @param {Number} wait - 延迟时间
 * @returns
 */
export function debounce(wait= 2000) {
  return function(target, name, descriptor) {
    const fn = descriptor.value
    let timer = null
    descriptor.value = function(...args) {
      const _this = this._isVue ? this : target
      clearTimeout(timer)
      timer = setTimeout(() => {
        fn.apply(_this, args)
      }, wait)
    }
  }
}
/**
 * 表单校验
 * @param {String} formElKey - 表单el
 */
export const formValidation = (formElKey = 'formEl') => {
  return (target, name, descriptor) => {
    const method = descriptor.value
    descriptor.value = async function() {
      const _this = this._isVue ? this : target
      const isValidate = _this[formElKey]?.validate
      if (isValidate) {
        const [, res] = await to(isValidate())
        if (!res) return false
      }
      return method.apply(_this, arguments)
    }
  }
}
// utils/decorator.js
/**
 * 确认框
 * @param {String} title - 标题
 * @param {String} concent - 内容
 * @param {String} confirmButtonText - 确认按钮名称
 * @returns
 */
export const alertDecorator = ({title = '提示', message = '请输入弹窗内容', confirmButtonText = '我知道了'}) => {
  return (target, name, descriptor) => {
    const fn = descriptor.value;
    descriptor.value = function (...args) {
        Dialog.alert({title, message, confirmButtonText}).then(() => {
          fn.apply(this, args);
        });
    }
  }
}



/**
 * 缓存计算结果
 * @export
 * @param {Function} fn
 * @returns
 */
export function cached() {
  return function(target, name, descriptor) {
    const method = descriptor.value
    const cache = new Map()
    descriptor.value = function() {
      const _this = this._isVue ? this : target
      const key = JSON.stringify(arguments)
      if (!cache.has(key)) {
        cache.set(key, method.apply(_this, arguments))
      }
      return cache.get(key)
    }
  }
}
Now that you see this, let’s collect it first. If the actual paddling time improves, don’t forget to come back and give a thumbs up.
If you find it useful, share it with your friends!
Next is a happy paddling O(∩_∩)O haha~

CoolLsk
119 声望11 粉丝

座右铭是树袋熊