Manipulating props using default in vuejs

Viewed 55

Basically, I have very less edge cases where I need to change the value of props on init like so,

props : {
    columnName : String,
    setValue: {

        validator: function (value) {
            //enum edge cases
            let _value = value;
            if(value === 'YES' || value === 'ACTIVE'){
                value = 0;
            }
            else if(value === 'NO' || value === 'VOID'){
                value = 1;
            }
            console.log(_value);
            return _value;
        }
    }
},

Is this possible, I did try this but it is still sending the actual values instead of 0/1.

1 Answers

You could try to return 'value' rather than '_value'. However, I believe this is a job for a computed property, rather than trying to manipulate the prop directly.

computed:{
  computedSetValue(){
    if(this.setValue === 'YES' || this.setValue === 'ACTIVE'){
      return 0
    }
    else if(this.setValue=== 'NO' || this.setValue=== 'VOID'){
      return 1
    } 
    return 0      
  }
}

Then, you may use this.computedSetValue as you do this.setValue

Related