优化后的代码如下:

showMsg({ title, type, url, icon = true }) { const params = url ? { title, type, url, icon } : { title, type }; return this.$refs.uToast.show(params); }

优化点:

  1. 使用 const 替代 let,因为 params 的值不会改变。
  2. 使用三目运算符替代 if 语句,使代码更简洁。
  3. 直接在对象字面量中定义 params 的值,避免了先定义 params 再进行判断的操作。
Vue 函数优化:使用三目运算符和 const 简化代码

原文地址: https://www.cveoy.top/t/topic/mKT5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录