[Vuejs]-Vue Array from store is not available (in time) in component

0👍

If gameCards array is not populated at your component’s mount time, you can use a watcher to run your functions whenever gameCards is actually ready to work with

watch: {
    // whenever gameCards is assigned a new value, this function will run
    gameCards(newValue, oldValue) {
      // only do this after first assignment (when gameCards is initially empty)
      if(oldValue.length === 0) {
        this.createDeck()
        this.createShowCards()
      }
    }
  },

The watcher will always run when a new array value is assigned, but as my code example shows, you can control whether or not the watcher actually does anything by using logic based on the new and old array values passed in.

While the watcher by default only runs on complete reassignments of the watched array, you can have it run whenever an individual array object is modified by using a deep watcher instead (if that’s something you need).

Leave a comment