Answer the question
In order to leave comments, you need to log in
Vuejs: Component doesn't update data when store data changes. What could be the reason?
Hello. Such a problem: there is a certain component:
<template>
<div>
<ul v-for="(user, index) in users.items" :key="index">
<li>{{ user.name }}</li>
</ul>
</div>
</template>
<script>
export default {
name: 'UserList',
mounted() {
this.fetchUsers()
},
computed: {
users() {
return this.$store.getters.users
}
},
methods: {
fetchUsers() {
this.$store.dispatch('FETCH_USERS')
}
}
}
</script>
import Vue from 'vue'
import Vuex from 'vuex'
import Users from './store/users'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Users }
})
// А это сам модуль "Users"
const state = {
users: {
items: [],
total_items: 0
}
}
const getters = {
users: state => state.users
}
const actions = {
FETCH_USERS ({commit}) {
fetch(new URL('...'))
.then(response => response.json())
.then(response => commit('SET_USERS', response))
.catch(...)
}
}
const mutations = {
SET_USERS (state, users) {
state.users = users
}
}
export default {
state,
getters,
actions,
mutations
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question