Vue 函数优化:使用三目运算符和 const 简化代码
优化后的代码如下:
showMsg({ title, type, url, icon = true }) { const params = url ? { title, type, url, icon } : { title, type }; return this.$refs.uToast.show(params); }
优化点:
- 使用 const 替代 let,因为 params 的值不会改变。
- 使用三目运算符替代 if 语句,使代码更简洁。
- 直接在对象字面量中定义 params 的值,避免了先定义 params 再进行判断的操作。
原文地址: https://www.cveoy.top/t/topic/mKT5 著作权归作者所有。请勿转载和采集!