0

I want to initialise selectedElement with the id of the elements object in vuejs.

This is not working:

data() {
 return {
    elements: [
      { id: 0, name: 'Simple elements' },
      { id: 1, name: 'Call to action' },
    ],
    selectedElement: elements[0]
   };
 },
lopezi
  • 537
  • 2
  • 7
  • 20
  • You can't do it *within* the object literal (see [these answers](https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers)), you'll have to do something like `const elements = [/*...*/]; return { elements, selectedElement: elements[0] };`. – T.J. Crowder Aug 31 '22 at 12:03
  • have a look at this [answer](https://stackoverflow.com/questions/46491468/how-to-put-a-value-of-data-object-in-another-data-object-vuejs) – Alan Omar Aug 31 '22 at 12:04
  • If selectedElement should always default to elements[0] when undefined, this can be done in a computed: `return this.privateSelectedElement || this.elements[0]` – Estus Flask Aug 31 '22 at 12:05

0 Answers0