小胖墩er 小胖墩er
首页
  • 前端文章

    • JavaScript
    • Vue
    • ES6
    • Git
  • Vue
  • React
  • HTML
  • CSS
  • 工具类
  • GitHub技巧
  • 博客搭建
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小胖墩er

Better later than never.
首页
  • 前端文章

    • JavaScript
    • Vue
    • ES6
    • Git
  • Vue
  • React
  • HTML
  • CSS
  • 工具类
  • GitHub技巧
  • 博客搭建
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 工具类

  • 问题技巧以及优化

    • 问题总结

    • 优化技巧

      • axios发送get请求url传递字符时需要进行编码
      • 当选择某一项时,需要把指定项的值存储起来后使用
      • 优化&运算
      • 优化if语句中相同的代码案例
      • 解决v-for和v-if不能同时使用问题
      • 优化从两个数组中循环判断相等取值操作
  • 博客搭建

  • GitHub技巧

  • 技术
  • 问题技巧以及优化
  • 优化技巧
小胖墩er
2021-09-09

优化if语句中相同的代码案例

优化前

if ([0, 1].includes(loadType)) {
  this.energyData = res.data.energyItems;
  this.energyArr = this.energyData.length > 9 ? this.energyData.slice(0, 9) : (this.energyArr = this.energyData)
  this.getEnergyRank()
}
if ([0, 2].includes(loadType)) {
  this.constData = res.data.energyItems;
  this.costArr = this.constData.length > 9 ? this.constData.slice(0, 9) : (this.costArr = this.constData)
  this.costRank()
}
1
2
3
4
5
6
7
8
9
10

优化后

function handle({thisKey,dataKey,thisArr}) {
  this[thisKey] = res.data[dataKey];
  this[thisArr] = this[thisKey].length > 9 ? this[thisKey].slice(0, 9) : (this[thisArr] = this[thisKey])
}


let key = {
  1: {
    dataKey: 'energyItems',
    thisKey: 'energyData',
    thisArr: 'energyArr'
  },
  2: {
    dataKey: 'costItems',
    thisKey: 'constData',
    thisArr: 'costArr'
  }
}

if ([0, 1].includes(loadType)) {
  this.handle(key[1])
  this.getEnergyRank()
}
if ([0, 2].includes(loadType)) {
  this.handle(key[2])
  this.costRank()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
在线编辑 (opens new window)
#技巧总结
上次更新: 2021/11/14, 07:48:46
优化&运算
解决v-for和v-if不能同时使用问题

← 优化&运算 解决v-for和v-if不能同时使用问题→

最近更新
01
毛玻璃效果
11-23
02
svg基本绘制
11-23
03
滑动登录界面
11-23
更多文章>
🖥️

© 2021 小胖墩er 💌 粤ICP备2021158933号 🛀 Theme by 💝 Vdoing

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×