Home > Mobile >  How to merge two computed properties into one
How to merge two computed properties into one

Time:01-12

Sorry for the noob question, but how do I combine the two computed properties, without duplicating the code? Thanks a lot

computed: {
        getStatusColor() {
            const { end_of_availability } = this.fullData
            if (end_of_availability === 'unlimited') {
                return 'w-green'
            } else {
                if (moment.utc().diff(end_of_availability   'T23:59:59Z') < 0) return 'w-green'
                else return 'w-red'
            }
        },
        capitalizedAvailability() {
            const { end_of_availability } = this.fullData
            if (end_of_availability === 'unlimited') {
                return 'Active'
            } else {
                if(moment.utc().diff(end_of_availability   'T23:59:59Z') < 0) return 'Active'
                else return 'Inactive'
            }
        }
    } 

CodePudding user response:

What you seem to be interested in is knowing if your thing is active or not, so create a computed property that calculates if your thing is active, and return whatever you need to return based on that.

computed: {
  isActive() {
    const { end_of_availability } = this.fullData;
    return (
      end_of_availability === 'unlimited' ||
      moment.utc().diff(end_of_availability   'T23:59:59Z') < 0
    );
  },

  statusColor() {
    return this.isActive ? 'w-green' : 'w-red';
  },
  capitalizedAvailability() {
    return this.isActive ? 'Active' : 'Inactive';
  }
}

Also, name your computed properties as if they are properties, not as if they are methods. getStatusColor is not a property of a thing. The corresponding property would be statusColor.

  •  Tags:  
  • Related