最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if Vuex state object is empty - Stack Overflow

programmeradmin0浏览0评论

I have an initial empty object in Vuex state that gets updated from an API.

const state = {
  someObject: {}
}

How do I check if the object is empty in my template?

<template>
  <div v-if="someObject">
    This should not display when someObject is empty.
  </div>
</template>

What is best practice for checking if a state object is set/empty or not?

Should i set someObject: null/undefined/false initially, even if it expects to be updated with a new object?

Does it make sense to do a check in getters?

export const someObject = state => Object.getOwnPropertyNames(state.someObject).length == 0 ? state.someObject : false

I have an initial empty object in Vuex state that gets updated from an API.

const state = {
  someObject: {}
}

How do I check if the object is empty in my template?

<template>
  <div v-if="someObject">
    This should not display when someObject is empty.
  </div>
</template>

What is best practice for checking if a state object is set/empty or not?

Should i set someObject: null/undefined/false initially, even if it expects to be updated with a new object?

Does it make sense to do a check in getters?

export const someObject = state => Object.getOwnPropertyNames(state.someObject).length == 0 ? state.someObject : false
Share Improve this question edited Jul 6, 2017 at 22:12 Andreas asked Jul 6, 2017 at 21:35 AndreasAndreas 1,0991 gold badge11 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

You could use the lodash method: _.isEmpty({someObject});

Or if you wanted to do a getter:

computed:{
  objectLength(state){
    return Object.keys(state.someObject).length
  }

Depending on particular use case I would ether set it to null/undefined or make if check by some required object property like v-if='someObject.id'

Anything else seams as unnecessary complication.

发布评论

评论列表(0)

  1. 暂无评论