vue 判断数据是否为空


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
28
29
30
31
32
33
34
<template>
  <div>数据是否为空</div>
</template>

<script>
export default {
  name: "Orders",
  data() {
    return {
      abj: {},
      arr: []
    };
  },
  created() {
    //判读数组是否为空
    if(this.arr == undefined || this.arr.length <= 0) {
      console.log("数组为空");
    }
    if(this.arr !== undefined && this.arr.length > 0) {
      console.log("数组不为空");
    }

    //判断对象是否为空
    if(JSON.stringify(this.obj) === "{}"){
      console.log("对象为空");
    } else {
      console.log("对象不为空");
    }
  },
  methods: {},
};
</script>

<style lang="scss" scoped></style>

1.数组为空

1
this.arr == undefined || this.arr.length <= 0

2.数组不为空

1
this.arr !== undefined && this.arr.length > 0

3.对象判断

1
JSON.stringify(this.obj) === "{}"