Accessing Vuex state object property from component
up vote
0
down vote
favorite
I'm trying to change value of a state object property from a component but I can't find the proper way to do it
Here is my state:
state: {
articles: [ {
title: "Lorem ipsum",
position: 7
}, {
title: "Lorem ipsum",
position: 8
}
]
}
In the computed property of the component :
return this.$store.state.articles.filter((article) => {
return (article.title).match(this.search); /// just a search field, don't mind it
});
I would like to change every article.position under 10 to article.position = +'0'article.position.
I tried :
let articlesList = this.$store.state.articles
if(articlesList.position < 10) {
articlesList.position = +'0'articlesList.position
}
I know this is not the good way to do it but this is all I have. Any tips? :)
vue.js vuex
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I'm trying to change value of a state object property from a component but I can't find the proper way to do it
Here is my state:
state: {
articles: [ {
title: "Lorem ipsum",
position: 7
}, {
title: "Lorem ipsum",
position: 8
}
]
}
In the computed property of the component :
return this.$store.state.articles.filter((article) => {
return (article.title).match(this.search); /// just a search field, don't mind it
});
I would like to change every article.position under 10 to article.position = +'0'article.position.
I tried :
let articlesList = this.$store.state.articles
if(articlesList.position < 10) {
articlesList.position = +'0'articlesList.position
}
I know this is not the good way to do it but this is all I have. Any tips? :)
vue.js vuex
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion withstore.commit('ajustPosition')
– wheeler
Nov 10 at 14:44
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to change value of a state object property from a component but I can't find the proper way to do it
Here is my state:
state: {
articles: [ {
title: "Lorem ipsum",
position: 7
}, {
title: "Lorem ipsum",
position: 8
}
]
}
In the computed property of the component :
return this.$store.state.articles.filter((article) => {
return (article.title).match(this.search); /// just a search field, don't mind it
});
I would like to change every article.position under 10 to article.position = +'0'article.position.
I tried :
let articlesList = this.$store.state.articles
if(articlesList.position < 10) {
articlesList.position = +'0'articlesList.position
}
I know this is not the good way to do it but this is all I have. Any tips? :)
vue.js vuex
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to change value of a state object property from a component but I can't find the proper way to do it
Here is my state:
state: {
articles: [ {
title: "Lorem ipsum",
position: 7
}, {
title: "Lorem ipsum",
position: 8
}
]
}
In the computed property of the component :
return this.$store.state.articles.filter((article) => {
return (article.title).match(this.search); /// just a search field, don't mind it
});
I would like to change every article.position under 10 to article.position = +'0'article.position.
I tried :
let articlesList = this.$store.state.articles
if(articlesList.position < 10) {
articlesList.position = +'0'articlesList.position
}
I know this is not the good way to do it but this is all I have. Any tips? :)
vue.js vuex
vue.js vuex
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 10 at 14:24
Arkaer
211
211
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Arkaer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion withstore.commit('ajustPosition')
– wheeler
Nov 10 at 14:44
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48
add a comment |
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion withstore.commit('ajustPosition')
– wheeler
Nov 10 at 14:44
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion with
store.commit('ajustPosition')– wheeler
Nov 10 at 14:44
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion with
store.commit('ajustPosition')– wheeler
Nov 10 at 14:44
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You should put all Vuex state change code into the Vuex store itself, via a mutation or an action. Mutations are for synchronous data changes that will update all reactive components. Actions are for asynchronous events, such as persisting state changes to the server.
So, when the change occurs, it should call a method on your component, which should in turn call the appropriate mutation or action on the Vuex store.
You don't give your full code, but here is a hypothetical example:
(Edited to reflect question about accessing data from within the mutation method.)
Template:
<input type="text" name="position" :value="article.position" @input="updatePosition">
Method on Component:
methods: {
updatePosition(e) {
this.$store.commit('changeArticleOrder', article.position, e.target.value)
}
}
Mutation on Vuex Store:
mutations: {
changeArticleOrder(state, oldPosition, newPosition) {
for (var i = 0; i < 10; i++) {
state.articles[i].position = i + 1;
/* Or do whatever it is you want to do with your positioning. */
}
}
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
First argument of mutation is astateobject, that should be used to mutate state. More in Vuex documentation
– aBiscuit
Nov 10 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You should put all Vuex state change code into the Vuex store itself, via a mutation or an action. Mutations are for synchronous data changes that will update all reactive components. Actions are for asynchronous events, such as persisting state changes to the server.
So, when the change occurs, it should call a method on your component, which should in turn call the appropriate mutation or action on the Vuex store.
You don't give your full code, but here is a hypothetical example:
(Edited to reflect question about accessing data from within the mutation method.)
Template:
<input type="text" name="position" :value="article.position" @input="updatePosition">
Method on Component:
methods: {
updatePosition(e) {
this.$store.commit('changeArticleOrder', article.position, e.target.value)
}
}
Mutation on Vuex Store:
mutations: {
changeArticleOrder(state, oldPosition, newPosition) {
for (var i = 0; i < 10; i++) {
state.articles[i].position = i + 1;
/* Or do whatever it is you want to do with your positioning. */
}
}
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
First argument of mutation is astateobject, that should be used to mutate state. More in Vuex documentation
– aBiscuit
Nov 10 at 16:25
add a comment |
up vote
0
down vote
You should put all Vuex state change code into the Vuex store itself, via a mutation or an action. Mutations are for synchronous data changes that will update all reactive components. Actions are for asynchronous events, such as persisting state changes to the server.
So, when the change occurs, it should call a method on your component, which should in turn call the appropriate mutation or action on the Vuex store.
You don't give your full code, but here is a hypothetical example:
(Edited to reflect question about accessing data from within the mutation method.)
Template:
<input type="text" name="position" :value="article.position" @input="updatePosition">
Method on Component:
methods: {
updatePosition(e) {
this.$store.commit('changeArticleOrder', article.position, e.target.value)
}
}
Mutation on Vuex Store:
mutations: {
changeArticleOrder(state, oldPosition, newPosition) {
for (var i = 0; i < 10; i++) {
state.articles[i].position = i + 1;
/* Or do whatever it is you want to do with your positioning. */
}
}
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
First argument of mutation is astateobject, that should be used to mutate state. More in Vuex documentation
– aBiscuit
Nov 10 at 16:25
add a comment |
up vote
0
down vote
up vote
0
down vote
You should put all Vuex state change code into the Vuex store itself, via a mutation or an action. Mutations are for synchronous data changes that will update all reactive components. Actions are for asynchronous events, such as persisting state changes to the server.
So, when the change occurs, it should call a method on your component, which should in turn call the appropriate mutation or action on the Vuex store.
You don't give your full code, but here is a hypothetical example:
(Edited to reflect question about accessing data from within the mutation method.)
Template:
<input type="text" name="position" :value="article.position" @input="updatePosition">
Method on Component:
methods: {
updatePosition(e) {
this.$store.commit('changeArticleOrder', article.position, e.target.value)
}
}
Mutation on Vuex Store:
mutations: {
changeArticleOrder(state, oldPosition, newPosition) {
for (var i = 0; i < 10; i++) {
state.articles[i].position = i + 1;
/* Or do whatever it is you want to do with your positioning. */
}
}
You should put all Vuex state change code into the Vuex store itself, via a mutation or an action. Mutations are for synchronous data changes that will update all reactive components. Actions are for asynchronous events, such as persisting state changes to the server.
So, when the change occurs, it should call a method on your component, which should in turn call the appropriate mutation or action on the Vuex store.
You don't give your full code, but here is a hypothetical example:
(Edited to reflect question about accessing data from within the mutation method.)
Template:
<input type="text" name="position" :value="article.position" @input="updatePosition">
Method on Component:
methods: {
updatePosition(e) {
this.$store.commit('changeArticleOrder', article.position, e.target.value)
}
}
Mutation on Vuex Store:
mutations: {
changeArticleOrder(state, oldPosition, newPosition) {
for (var i = 0; i < 10; i++) {
state.articles[i].position = i + 1;
/* Or do whatever it is you want to do with your positioning. */
}
}
edited Nov 10 at 18:38
answered Nov 10 at 14:58
bluejack
17812
17812
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
First argument of mutation is astateobject, that should be used to mutate state. More in Vuex documentation
– aBiscuit
Nov 10 at 16:25
add a comment |
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
First argument of mutation is astateobject, that should be used to mutate state. More in Vuex documentation
– aBiscuit
Nov 10 at 16:25
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
Thank you for your help. I just want to change all the article.position when they are < 10, without an event. I get them from an API in an action, and I should use a mutation to change the data. How can I access the state.articles.position in a mutation?
– Arkaer
Nov 10 at 15:47
1
1
First argument of mutation is a
state object, that should be used to mutate state. More in Vuex documentation– aBiscuit
Nov 10 at 16:25
First argument of mutation is a
state object, that should be used to mutate state. More in Vuex documentation– aBiscuit
Nov 10 at 16:25
add a comment |
Arkaer is a new contributor. Be nice, and check out our Code of Conduct.
Arkaer is a new contributor. Be nice, and check out our Code of Conduct.
Arkaer is a new contributor. Be nice, and check out our Code of Conduct.
Arkaer is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239894%2faccessing-vuex-state-object-property-from-component%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
you should change the state only with mutations. (vuex.vuejs.org/guide/mutations.html) then you can call the mutaion with
store.commit('ajustPosition')– wheeler
Nov 10 at 14:44
Thank you for your help. How should I access to the state.articles.position in the mutation?
– Arkaer
Nov 10 at 15:48