[Vuejs]-Vue, How to Normalizing Complex Data and assigning unique Ids

0👍

Vue.set is the preferred method of adding new properties to existing object, Object.assign being another:

import Vue from 'vue'

// define an initial object state
const defaultRow = {
  inputBudget: '',
  amountBudgeted: 0,
  remaining: 0
}

const state = {
  budgetRows: {}
}

const mutations = {
  createRow (state) {
    const uid = uniqId()

    // with Vue.set
    Vue.set(state.budgetRows, uid, defaultRow)

    // with Object.assign()
    Object.assign(state.budgetRows, { [uid]: defaultRow })
  }
}

Leave a comment