Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object assignment in Vue.js method

I have a Vue component in which I select a specific value from an array of objects then attempt to copy some fields from that value into Vue data

  <div class="container">
    <h4>Add Item</h4>
    <form @submit.prevent="addItem(item.Code)">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label for="ItemCode">Code</label>&nbsp;
            <select
              id="ItemCode"
              v-model="item.Code"
            >
              <input
                v-model="item.Code"
                type="hidden"
              >
              <option
                v-for="part in PartCodes"
                :key="part"
              >
                {{ part }}
              </option>
            </select>
   .
   .
   .
    </form>
  </div>

where the data is

  data() {
    return {
      item: {},
      parts: [],
    };
  },
  computed: {
    PartCodes: function () {
      return [...new Set(this.parts.map(p => p.Code))];
    },
  },
  created() {
    let uri = '/parts';
    if (process.env.NODE_ENV !== 'production') {
      uri = 'http://localhost:4000/parts';
    }
    this.axios.get(uri).then(response => {
      this.parts = response.data;
    });
  },
  methods: {
    addItem(selectCode) {
      let uri = '/items/create';
      if (process.env.NODE_ENV !== 'production') {
        uri = 'http://localhost:4000/items/create';
      }
      let selectPart = this.parts.filter( obj => {
        return obj.Code === selectCode;
      });

      this.item.Description = selectPart.Description;
      this.item.Cost = selectPart.Cost;
      this.item.Price = selectPart.Price);

      this.axios.post(uri, this.item)
        .then(() => {
          this.$router.push({name: 'QuoteIndex'});
        });
    }
  }
};

When I log the object 'selectPart' it has the correct fields but assigning these fields into the object 'items' results in 'undefined' values

I must be doing something wrong with scope but I don't know what is wrong.

Please suggest how I can copy fields with this Component

Thanks enter image description here

like image 617
Des Albert Avatar asked Oct 24 '25 07:10

Des Albert


1 Answers

In Vue 2.x, properties added to objects are not reactive. You have declared the item data item without the properties Description and Price, and have later assigned these properties using simple object assignment, which Vue will not be able to track.

There are two ways to solve this:

1. Declare all reactive properties upfront

Change data to

data() {
    return {
      item: {
        Description: null,
        Price: null
      },
      parts: [],
    };
  },

2. Use Vue.set()

Change

this.item.Description = selectPart.Description;
this.item.Price = selectPart.Price;

to

this.$set(this.item, 'Description', selectPart.Description);
this.$set(this.item, 'Price', selectPart.Price);

Thankfully in Vue 3.x this caveat will be eliminated and all properties added to reactive objects will themselves become reactive.

like image 72
Daniel Elkington Avatar answered Oct 25 '25 20:10

Daniel Elkington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!