[Vuejs]-Vue Dynamic Form Values AND Keys, with pre existing value for render, why not rendering?

0👍

To set a dynamic key name on an object in javascript, it turns out you can use square brackets, as in:

{
 [keyName] : value
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names

So my code is fixed by simply passing the prop as the key in the axios call:

submit() {
      axios
        .post(`/admin/${this.userId}/update`, {
          [this.$props.field]: this.value
        })
        .then(response => {
          // we out here
        });
    }

Leave a comment