Answer the question
In order to leave comments, you need to log in
Computed is not updated when state changed?
I know that computed cached , and will be changed when the state is changed, but for some reason my state is updated, and computed remains old.
Here are the pieces of code, the whole code will not work out, too much.
const state = () => {
items: [],
};
export default state;
[MUTATION_LOAD_LAST_COMMENTS] (state, {itemLoopIndex}) {
// @todo - this is dummy data, should come from API
const randomTrueFalse = Math.floor(Math.random() * 2) + 1;
const comments = [];
if (!state.items[`'${itemLoopIndex}'`] && 0 === randomTrueFalse % 2) {
for (let iterator = 0; iterator < 5; iterator++) {
comments.push(
{
id: iterator,
author: {
id: 1,
fullName: 'Andrew Stark',
avatarUrl: 'https://cdn.vuetifyjs.com/images/lists/2.jpg',
},
createdAt: iterator + ' hours ago',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ' +
'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation' +
' ullamco laboris nisi ut aliquip ex ea commodo consequat.',
},
);
}
}
state.items[`'${itemLoopIndex}'`] = comments;
},
export default mutations;
const getters = {
[GET_IS_COMMENTS_LOADED]: state => ({itemLoopIndex}) => {
return !!state.items[itemLoopIndex];
},
};
export default getters;
computed: {
...mapGetters(
LISTS,
{
isCommentsLoaded: `${LIST_POSTS_COMMENTS}/${GET_IS_COMMENTS_LOADED}`,
}
),
},
Answer the question
In order to leave comments, you need to log in
Start by defining the problem. The design must fulfill the stated goals. If it doesn’t, then you need to first determine them, so that there is something to solve. If the objectives are met, then "design for the sake of design should not be encouraged (c)".
Telecom engineer and programmer advise in design. I doubt that "something" will come of this. Do not change, but demolish and start from a blank canvas
const state = () => {
items: [],
};
state.items[`'${itemLoopIndex}'`] = comments;
Vue cannot track the following changes to an array:
1. Directly setting an element by index:vm.items[indexOfItem] = newValue
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question