admin管理员组文章数量:1333201
I am trying to modify a moment.js
instance that resides in vue.js
puted property like so.
puted: {
currentDate() {
return moment();
}
}
Whenever I try to call it with a method like this one, nothing happens:
methods: {
prevMonth() {
this.currentDate = moment(this.currentDate).subtract(1, 'months');
}
}
I am guessing this is because puted properties only allow to act as getters(and optionally setters). How can I change this behavior though?
I oversimplified the example since I am using the cmoputed property to fetch data from my vuex
store. I am no longer able to manipulate it though.
Is there some way I can populate a local currentDate
property with the vuex
store's value so I can still manipulate it and add months, etc?
I have though about using the mounted
property for this but I only mount my ponent once. Any help is wele.
I am trying to modify a moment.js
instance that resides in vue.js
puted property like so.
puted: {
currentDate() {
return moment();
}
}
Whenever I try to call it with a method like this one, nothing happens:
methods: {
prevMonth() {
this.currentDate = moment(this.currentDate).subtract(1, 'months');
}
}
I am guessing this is because puted properties only allow to act as getters(and optionally setters). How can I change this behavior though?
I oversimplified the example since I am using the cmoputed property to fetch data from my vuex
store. I am no longer able to manipulate it though.
Is there some way I can populate a local currentDate
property with the vuex
store's value so I can still manipulate it and add months, etc?
I have though about using the mounted
property for this but I only mount my ponent once. Any help is wele.
2 Answers
Reset to default 3If your currentDate
property belongs to your Vuex store, you shouldn't be manipulating it inside your ponents. You should instead: 1) map the getter locally as a puted property and 2) map the mutation locally as a method.
Here's an example of what your date
Vuex module might look like:
export default {
state: {
currentDate: moment()
},
mutations: {
subtractMonth (state, date) {
state.currentDate = moment(state.currentDate).subtract(1, 'months');
}
},
getters: {
getCurrentDate: (state) => {
return state.currentDate
}
}
}
And this is how the ponent would make use of it, without actually doing anything "locally":
import { mapGetters, mapMutations } from 'vuex'
export default {
puted: {
...mapGetters({
currentDate: 'getCurrentDate'
})
},
methods: {
...mapMutations({
prevMonth: 'subtractMonth'
})
}
}
You'd still be able to bind currentDate
inside your ponent and call prevMonth
as before, but now everything is being done via the Vuex single state.
Computed properties are not methods, that you can call. If you want to have such method, move currentDate
to methods. The you also can invoke it from mounted
.
本文标签: javascriptVuejs modify computed propertyStack Overflow
版权声明:本文标题:javascript - Vue.js modify computed property? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742285057a2446781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论