Calling function shortly after page load, once results are received from controller

Viewed 38

I'm trying to call a computed property (which relies on a data variable to be set from incoming json) upon page creation but it says that it can't read the data for 'undefined'. This is due to the data I need not being fully loaded, I'm pretty sure. When I set it to run 2 seconds after created(), it works just fine.

How can I neatly set this to only run the function on my data variable one time but ONLY once the data results are actually set?

<div id="app">
  <div v-for="(value, employee) in employeeFunction" :key="employee">
    @{{value}}
  </div>
</div>


<script>

var vm = 
new Vue({
  el: "#app",

  data: {

    results: [
        {//data is in here

        }
    ],

  },
  created: function(){
    this.interval = setInterval(() => this.employeeFunction(), 2000);
  },
  computed: {

    employeeFunction() {
      console.log('employee data')
      employeeResults = this.results
      console.log(employeeResults)
      return employeeResults
    },

    
  }
});
</script>
1 Answers

Instead of using the created() lifecycle-hook try with mounted()

Related