【发布时间】:2018-11-30 19:54:45
【问题描述】:
我是 Vue 和 Vuex 的新手,我正在尝试弄清楚如何改变存储中的数据。
目前在我的州,默认用户名是“default”,我想将其更改为“otto”,稍后我会从数据库中获取此信息。但为了理解的目的,我只是想让它发挥作用。
此时组件已正确加载,并显示为“默认”。不存在任何错误。
store.js:
// store.js
export default {
state: {
username: 'default'
},
getters: {
},
mutations: {
changeUsername (state) {
state.username = 'otto'
}
},
actions: {
changeUsername (context) {
context.commit('changeUsername')
}
}
}
vue 文件:
// Basic.vue
<template>
<div>
<p>{{ username }}</p>
</div>
</template>
<script>
import { mapState } from 'vuex';
import { mapMutations } from 'vuex';
import { mapActions } from 'vuex';
export default {
name: "basic",
computed: {
...mapState([
'username'
])
},
methods: {
...mapMutations([
'changeUsername'
]),
...mapActions([
'changeUsername'
])
}
}
</script>
【问题讨论】:
-
你在哪里调用突变/动作?
-
@ThomasKleßen 我不知道在哪里调用突变/动作,以及如何..
标签: vue.js vuejs2 vue-component vuex