关于return的使用问题
废话不多少,看代码体会
不使用return
        add() {
          if (this.brandName === '') {
            alert('未填写品牌名称')
          } else {
            // 创建模拟数据
            const obj = {
              id: 4,
              brand: this.brandName,
              status: false,
              buildTime: new Date()
            }
            // 添加数据
            this.list.push(obj)
            // 收尾处理
            this.brand = ""
            this.nextid++
          }
        }
使用return
        // 即阻止默认提交行为后,触发add方法
        add() {
          if (this.brandName === '') {
            return alert('未填写品牌名称')
          }
          // 创建模拟数据
          const obj = {
            id: 4,
            brand: this.brandName,
            status: false,
            buildTime: new Date()
          }
          // 添加数据
          this.list.push(obj)
          // 收尾处理
          this.brand = ""
          this.nextid++
        }
总结
  经过对比发现:使用return后,后续代码不会有太多的约束感。推荐使用return🤔。
完整的Demo代码:
地址:
 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号