Ott*_*tto 6 vue.js vue-component vuex vuejs2
我是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')
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
<template>
<div>
<p @click="changeUsername">{{ username }}</p>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
如果用户像这样单击 p 标签,您可以更改用户名。
| 归档时间: |
|
| 查看次数: |
6241 次 |
| 最近记录: |