判断对象里面的数据是否为空 Object.keys(xxxxx)

要求

如果对象里面的数据为空 显示弹出框添加数据 存在数据时则提示“存在数据无需添加”

demo
<!-- 按钮 -->
    <div class="btn">
      <sec-button
        type="primary"
        plain
        :disabled="disabled"
        @click="add" -------给按钮一个事件---------
      >新增</sec-button>
    </div>
script
//  点击
    add() {
    
    
      -------------判断对象的方法  Object.keys(xxxxx).length === 0----------------------
      if (Object.keys(this.tableData).length === 0) {
    
    
        this.title = '添加'
        this.dialogVisible = true
        this.form = {
    
    
          accessKey: '',
          encryptSecurity: '',
          url: ''
        }
      } else {
    
    
        this.disabled = true
        this.$message('已有数据,无需重新添加 ')
        console.log(this.tableData, 'tableData')
      }
    },

在vue中有两种方法可进行判断对象里面的数据是否为空

  1. JSON.stringify(evtValue)=='{}'
  2. Object.keys(xxx).length==0
    js判断对象是否为空对象的几种方法
    1.将json对象转化为json字符串,再判断该字符串是否为"{}" var data = {}; var b (JSON.stringify(data) == "{}"); alert(b);//true
    2.for in 循环判断

猜你喜欢

转载自blog.csdn.net/GikQxx21_wen/article/details/128804254