vuex的模板配置

2 篇文章 0 订阅

安装

 npm install vuex@3
在src下创建store文件夹,创建index.js,放入如下内容
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store({
  // 开启严格模式,任何对状态的更改如果不是通过mutation操作引起的,将会报警告
  strict: true,
  state: {
    count: 1,
  },

  getters: {
    doubleCount: (state) => state.count * 2,
  },

  mutations: {
    INCREASE_COUNT(state, val) {
      state.count += val;
    }
  },
  actions: {
    setCount({ commit }, data) {
      if (data.type === "+") {
        commit("INCREASE_COUNT", data.value);
      } else {
        commit("DECREASE_COUNT", data.value);
      }
    },
  },
});

export default store;
在main.js里加入如下元素
import store from '@/store/index.js';
new Vue({
	// 在根组件中挂载store实例:之后在任意的子组件中可以使用 this.$store访问store实例
	store,
	router,
	render: h => h(App),
}).$mount('#app');
在组件中引入

1.state

import {mapState} from 'vuex

computed: {
  // 使用对象展开运算符将此对象混入到外部对象中
  // 映射 this.count 为 store.state.count
  
  // 借助mapState生成计算属性:count、message(对象写法)--- 对象写法可以对属性进行重命名
  ...mapState({count:'count', message:'message'}),
    
  // 借助mapState生成计算属性:count、message(数组写法)--- 需要和state中的属性名保持一致
  ...mapState(['count', 'message']),
}

2.getters

import {mapGetters} from 'vuex

computed: {
    // 借助mapGetters生成计算属性:bigCount(对象写法)
  ...mapGetters({bigCount:'bigCount'})
                 
  // 借助mapGetters生成计算属性:bigCount(数组写法)
  ...mapGetters(['bigCount']),
}

3.mutations

import { mapMutations } from 'vuex';

methods:{
  increase1() {
      // this.$store.commit('INCREASE_COUNT', 1);
      this.INCREASE_COUNT(1);
		},
  
  // 借助mapMutation生成函数:setCount(对象写法)
  ...mapMutations({INCREASE_COUNT:'INCREASE_COUNT'}),
  
  // 借助mapMutation生成函数:SET_COUNT(数组写法)
  ...mapMutations(['INCREASE_COUNT']),//把SET_COUNT映射为组件自己的函数
}

4.actions

import { mapActions } from 'vuex';

methods:{
  increase() {
    // this.$store.dispatch('setCount', { type: 0, value: 2 });
    this.setCount({ type: 0, value: 2 });
  },
      
  // 借助mapActions生成函数:setCount(对象写法)
  ...mapActions({setCount:'setCount'}),
  
  // 借助mapActions生成函数:setCount(数组写法)
  ...mapActions(['setCount']),
}

示例图

在这里插入图片描述

vuex模块化模板

index.js下

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

//import createLogger from 'vuex/dist/logger'

import setting from '@/store/modules/setting';
import user from '@/store/modules/user';

export default new Vuex.Store({
	state: {
	},
	getters: {
	},
	mutations: {
	},
	actions: {
	},
  modules: {
		user,
		setting,
  },
  //plugins: [createLogger()]

});

模块模板

export default {
  namespaced: true,
  state: {
    user:{},
    isLogin: false,
  },
  getters: {
    user: state=> state.user,
    isLogin: state=> state.isLogin,
  },
  mutations: {},
  actions:{}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寂173

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值