0

I have a component to which I pass data to via a method:

<div class="component-form" v-if="component">
    <render-component class="component-form__render-view"></render-component>
    <form-sidebar
      class="component-form__sidebar"
      :component="formatData()"
    >
    </form-sidebar>
</div>

This is said method (located in, well, methods):

formatData: function () {
  const componentObject = {
    ID: this.component.ID,
    Title: this.component.Title,
    Groups: []
  }

  for (let object in this.component.ParametersSpec) {
    let actualObject = this.component.ParametersSpec[object]
    actualObject.Title = object

    if (!componentObject.Groups[actualObject.group]) {
      componentObject.Groups[actualObject.group] = []
    }

    componentObject.Groups[actualObject.group].push(actualObject)
    console.log(componentObject)
  }

  return componentObject
}

FormatData is dependent on an API request:

mounted () {
api.components(routes.COMPONENT_API_URL).getOne({id: this.id})
  .then(response => { this.component = response.data })
},

Then in this form-sidebar component, I just have a simple {{ component }} to display the contents. The following is the output:

{ "ID": "service-pit", "Title": "Service Pit", "Groups": [] }

But, Groups is empty, which it shouldn't be, and certainly isn't in the console. Now I know this is a common symptom of passing data before it is properly returned. Since the console also says groups is empty before I open it (then it rechecks the contents and finds out it isn't).

But if the request wasn't finished yet, then how did it display the ID and Title correctly? And how can I make Vue update the data when the request is done?

4
  • Did u define component in data section? If so - how? Commented Jul 2, 2018 at 14:51
  • @Aldarund I just returned component: undefined Commented Jul 2, 2018 at 15:09
  • vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats - check this Commented Jul 2, 2018 at 15:23
  • can you upload your code to somewhere. Commented Jul 2, 2018 at 15:27

1 Answer 1

1
const componentObject = {
    ID: this.component.ID,
    Title: this.component.Title,
    Groups: []
  }

had to be:

const componentObject = {
    ID: this.component.ID,
    Title: this.component.Title,
    Groups: {}
  }

This took way too long for such a small problem..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.