0

When an event is triggered, I call a function that fills a variable and opens a modal from a child component. But, there, my new variable is empty, and if I close/re-open modal I have the data, so the data is loaded after.

I tried to load that child component after I have data, but no good till now.

Parent

  <p class="open-modal-button" @click="openUpdatesModal">
     <i class="fas fa-sync"></i>
     Check for updates
  </p>

  <check-for-updates-modal
    v-if="check_for_updates_values.account_name != ''"
    :modalUpdatesOpen="modalUpdatesOpen"
    :check_for_updates_values="check_for_updates_values"
  />

  data() {
    return {
      //code....
      check_for_updates_values: [],
      modalUpdatesOpen: false,
    };
  }

openUpdatesModal() {
  this.temporaryChecker();
},

temporaryChecker() {
  this.check_for_updates_values.account_name = this.account_name;
  this.check_for_updates_values.company_registration_number = this.company_registration_number;
  this.check_for_updates_values.registered_address = this.registered_address;

  this.modalUpdatesOpen = !this.modalUpdatesOpen;
},

Child

<b-col>{{check_for_updates_values.account_name}}</b-col>

  props: [
    "modalUpdatesOpen",
    "check_for_updates_values",
  ],

  watch: {
    modalUpdatesOpen() {
      this.checkForChanges();
      this.$bvModal.show("modal-check-for-updates");
    },
  },
5
  • 1
    How do you initialize your check_for_updates_values data variable in your component? Commented Sep 24, 2020 at 6:50
  • check_for_updates_values: [], should be an object considering your treating it like one Commented Sep 24, 2020 at 6:56
  • I didn't copy the whole code, because is a lot. I tried to copy only the essential here. If I do a console.log in my child watch, for check_for_updates_values , the data are there, but in template is still null. Commented Sep 24, 2020 at 6:59
  • 2
    Like said above, check_for_updates_values = {} in your data then use this.$set(check_for_updates_values, 'account_name', this.account_name) cos your object is not reactive to it's properties changing since they don't exist at initialization Commented Sep 24, 2020 at 7:00
  • @SélimAchour, you'r right, it with your code too. Thank you! Commented Sep 24, 2020 at 7:07

1 Answer 1

3

If you really initialize check_for_updates_values as an array, this is the problem. According to your usage, it should be an object.

Also, be careful to explicitly initialize every existing key on the object, or Vue won't be able to register them for reactivity watchers! That means if you have a data object empty foo: {}, any time you add a property, it won't refresh the vue foo.label = 'test' // won't render properly.

data() {
   return {
      check_for_updates_values: {
         account_name: null,
         company_registration_number: null,
         registered_address: null,
      },
   };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so that was the problem. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.