score:48

Accepted answer

note: at the bottom of this answer, see the general point i make about update/reactivity issues with vue.


now, about the question, based on the code you posted, considering the template:

<div v-for="video in videos" :key="video.id">

it picks the videos from:

 data () {
   return {
     videos: freevideo
   }
 }

although it initializes from freevideo, in nowhere in your code you show an update of videos.

solution:

you already have the state mapped in the getfreevideo computed:

computed: {
  ...mapstate(['getfreevideo'])
}

use it:

<div v-for="video in getfreevideo" :key="video.id">

update:

i'm setting videos in data() to getfreevideo in the store within the created() lifecycle:

    this.videos = this.getfreevideo

this is not enough to keep this.videos updated with whatever this.getfreevideo is. whenever something is set to this.getfreevideo it will only change this.getfreevideo, not this.videos.

if you want to automatically update this.videos whenever this.getfreevideo changes, create a watcher:

watch: {
  getfreevideo() {
    this.videos = this.getfreevideo
  }
}

and then keep using videos in the v-for:

<div v-for="video in videos" :key="video.id">



vue's reactivity

if your state is not getting updated in the view, perhaps you are not exploring vue at its best:

to have vue automatically react to value changes, the objects must be initially declared in data. or, if not, they must be added using vue.set().

see the comments in the demo below. or open the same demo in a jsfiddle here.

new vue({
  el: '#app',
  data: {
    person: {
      name: 'edson'
    }
  },
  methods: {
    changename() {
      // because name is declared in data, whenever it
      // changes, vue automatically updates
      this.person.name = 'arantes';
    },
    changenickname() {
      // because nickname is not declared in data, when it
      // changes, vue will not automatically update
      this.person.nickname = 'pele';
      // although if anything else updates, this change will be seen
    },
    changenicknameproperly() {
      // when some property is not initially declared in data, the correct way
      // to add it is using vue.set or this.$set
      vue.set(this.person, 'address', '123th avenue.');
      
      // subsequent changes can be done directly now and it will auto update
      this.person.address = '345th avenue.';
    }
  }
})
/* css just for the demo, it is not necessary at all! */
span:nth-of-type(1),button:nth-of-type(1) { color: blue; }
span:nth-of-type(2),button:nth-of-type(2) { color: red; }
span:nth-of-type(3),button:nth-of-type(3) { color: green; }
span { font-family: monospace }
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <span>person.name: {{ person.name }}</span><br>
  <span>person.nickname: {{ person.nickname }}</span><br>
  <span>person.address: {{ person.address }}</span><br>
  <br>
  <button @click="changename">this.person.name = 'arantes'; (will auto update because `name` was in `data`)</button><br>
  <button @click="changenickname">this.person.nickname = 'pele'; (will not auto update because `nickname` was not in `data`)</button><br>
  <button @click="changenicknameproperly">vue.set(this.person, 'address', '99th st.'); (will auto update even though `address` was not in `data`)</button>
  <br>
  <br>
  for more info, read the comments in the code. or check the docs on <b>reactivity</b> (link below).
</div>

to master this part of vue, check the official docs on reactivity - change detection caveats. it is a must read!

score:2

if the computed property isn't referenced (e.g. "used") somewhere in your template code vue will skip reactivity for it.

first it's a bit confusing the way you're structuring the store and the state properties.

i would:

1) have a "videos" property in the store state

2) initialise it as an empty array

3) on application start populate it correctly with the "load" defaults, with a mutation that pushes the "default" video to it

4) have the components mapgetter to it under the name of videos

5) whenever you load a component that "updates" the possible videos, then dispatch the action and call the appropriate mutation to substitute the store "videos" property

note: if components can have different "default" videos, then probably you'll want to have a videos property in the store that is initialised as false. this then allows you to have a computed property that uses the getter for the videos property in the store and in case it is false

what i mean is, for the first case

// store
state: {
  videos: []
}

getters: {
  videos(state) { return state.videos } 
}

//components

...
computed: {
  videos() {
    this.$store.getters.videos
  }
}

for the second case


// store
state: {
  videos: false
}

getters: { personal_videos(state) { return state.videos } }

//components
data() { return { default: default_videos } },
computed: {
  ...mapgetters([ 'personal_videos' ]),
  videos() {
    if (this.personal_videos) {
      return this.personal_videos
    } else {
      return this.default
    } 
  }

}

personal: give them better names -_- and the first option is the clearest

score:3

so turns out the whole issue was with video.js. i'm half tempted to delete this question, but i would not want anyone who helped to lose any points.

the solution and input here did help to rethink my use of watchers or how i was attempting this. i ended up just using the central store for now since it works fine, but this will need to be refactored later.

i had to just forego using video.js as a player for now, and a regular html5 video player works without issues.


Related Query

More Query from same tag