vuex配置-修改-传值

本文介绍了如何使用Vuex进行状态管理。首先通过npm安装Vuex,然后创建store目录,包括state.js(定义初始数据)、getters.js(获取数据)、mutations.js(异步修改数据)和actions.js(同步修改数据)。接着在main.js中引入store,并在组件中使用mapGetters、mapMutations和mapActions来映射和操作状态。示例展示了如何在组件中调用actions和mutations改变和获取state中的name、age、count等值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先安装vuex    -->

npm install vuex --save

主要用的方法

import {mapGetters, mapMutations, mapActions} from 'vuex';

下面我自己做的几个简单的学习小例子,一起学习下。


1 首先新建目录store



2-1

 state.js -->初始数据 --

const state = {
    name: 'weish',
    age: 22,
liyuefeng:'李越峰',
count:6
};


export default state;

2-2

getters.js-->获取数据

export const name = (state) => {
    return state.name;
}


export const age = (state) => {
    return state.age
}


export const other = (state) => {
    return `My name is ${state.name}, I am ${state.age}.`;
}


export const liyuefeng = (state) => {
    return state.liyuefeng
}
export const count = (state) => {
return state.count
 }

2-3

mutations.js   ----->异步修改数据

export default {
    //与下方 commit 中的 ‘increment' 相对应
increment(state){
 state.count ++;
},
increments(state){
state.count --;
},
fun(state, changName){
state.name = changName.myName;
}

};

2-4

actions.js 同步修改数据

export default {
    increment({
        commit,
        state
    }) {
        //提交一个名为 increment 的变化,名字可自定义,可以认为是类型名,与上方 mutations 中的 increment 对应
        //commit 提交变化,修改数据的唯一方式就是显式的提交 mutations
        commit('increment')
    },
    increments({
        commit,
        state
    }) {
        //提交一个名为 increment 的变化,名字可自定义,可以认为是类型名,与上方 mutations 中的 increment 对应
        //commit 提交变化,修改数据的唯一方式就是显式的提交 mutations
        commit('increments')
    },
fun({commit},date){
commit('fun', {myName: date});
}

};

2-5

 index.js 引入-配置

import vue from 'vue';
import vuex from 'vuex';
import state from './state.js';
import * as getters from './getters.js';
import mutations from './mutations.js';
import actions from './actions.js';
import createLogger from 'vuex/dist/logger'; // 修改日志


vue.use(vuex);


const debug = process.env.NODE_ENV !== 'production'; // 开发环境中为true,否则为false


export default new vuex.Store({
    state,
    getters,
    mutations,
    actions,
    plugins: debug ? [createLogger()] : [] // 开发环境下显示vuex的状态修改

});

3 main.js引入

import store from './store/index.js';

new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>'

})


4 新建组件


<template>
  <div class="hello">12
<button-counter ></button-counter>
<button @click="fun">按钮</button>
{{name}}{{age}}{{other}}{{liyuefeng}}{{count}}
<button @click="increment">增加</button>
<button @click="increments">减少</button>
<h1>{{count}}</h1>
  </div>
</template>


<script>
import buttonCounter from './buttonCounter.vue'
import {mapGetters, mapMutations, mapActions} from 'vuex';
export default {


  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
    }
  },
computed: {
...mapGetters([
"name",
"age",
"other",
"liyuefeng",
"count"
])
},
components:{
buttonCounter
},
methods: {
//mapMutations  mapActions`
...mapActions([
//该 increment 来自 store.js 中导出的 actions 和 mutations 中的 increment 
'increment',
"increments",
"fun"
]),
//如果需要向actions里面传值就手动调用
fun(){
this.$store.dispatch('fun',this.msg);
}
//...mapActions(['fun'])==      this.$store.dispatch('increment');
}
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值