[Vuejs]-VueJS: Can I declare dynamically in computed:?

0👍

It’s doable but not elegant.

Basically, generate an object dynamically and merge it into computed.

Here’s a simple example.

let dynamicComputed = {};
for (let i = 0; i < 3; i++) {
  dynamicComputed[`message` + i] = {
    get() {
      // get operation
    },
    set() {
      // set operation
    }
  };
}

export default {
  computed: {
    exampleComputed() {
      return "example Computed values";
    },
    ...dynamicComputed
  }
};

Leave a comment