vue中的状态管理应用

vue.js官网中生态系统中给我们提供一个官方的插件vuex,利用这个插件我们可以集中式管理数据,一处修改,多处应用,多个组件依赖同一状态。主要应用于开发中大型项目。

首先:npm i vuex -S

引入插件  import Vuex from 'vuex'

Vuex是个对象,相关成员如下

成员用途
Store类,构造状态管理的实例
mapActions函数,通讯工具
mapMutations函数,通讯工具
mapGetters函数,通讯工具
mapState函数,通讯工具

状态管理 store实例相关成员

成员用途
dispatch实例方法,通讯工具
commit实例方法,通讯工具
state属性,获取公共数据

交互流程

components->actions->mutations->state<-getters->->components
发送请求处理业务,异步修改状态状态读取处理状态渲染状态
dispatchcommitstate.key=value{key:value}state.key{{key}}
mapActions->    <-mapGetters
mapMutations->------------------->  <------------<-mapState
commit->------------------->    
    <------------<-state

基础应用:

1、新建一个js文件 :// src/plugins/vuex.js

//配置 store
import Vue from 'vue';
import Vuex from 'vuex';//引入vuex包
Vue.use(Vuex);//安装插件到vue

import state from '../store/state.js'
import actions from '../store/actions.js'
import mutations from '../store/mutations.js'
import getters from '../store/getters.js'

let store = new Vuex.Store({//配置接受state等选项,值为对象
  state,mutations,
  getters,actions
});

export default store;//导出store实例给main.js

2、在main.js引入store实例

import store from './plugins/vuex.js'

new Vue({
  render: h => h(App),
  store//控制vue实例,为vue实例提供一个状态管理实例,管理整个vue公共状态
}).$mount('#app')

3、在// src/store/目录下打造state.js 、actions.js、mutations.js、getters.js。

let state={
  count:0
}

export default state;
let actions = {
  add:({commit,state},payload)=>{
   	//state 公共状态 payload 负载
    commit('increment',payload)
  }
};
export default actions;
let mutations={
  increment:(state,payload)=>{
    //state 仓库|公共数据 payload携带的负载
    state.count+=payload;//mutations 不做业务,只负责突变state
  }
};

export default mutations;
let getters = {
  count:(state)=>{
    //返回处理后的state  ~~ computed  
    return state.count % 2 === 0 ? state.count + '偶数': state.count + '奇数'
  }
};
export default getters;

在组件中的应用

<!--声明式 发送请求-->
<div @click="类型(负载)"></div>

<!--展示状态-->
<div>{{$store.state.count}}</div>
<div>{{count}}</div>
  import {
		mapActions,
		mapMutations,
		mapState,
		mapGetters
	} from 'vuex'
	import store from '../plugins/vuex.js';
	export default {
		name: 'app',
		methods: {
			add() { //编程式 发送请求 
				store | this.$store.dispatch('add', 2) //-> actions
				//store | this.$store.commit('increment',2)    //-> mutations
				//store | this.$store.dispatch|commit({type:'类型',payload:负载})
			}
		},

		//mapActions 用来接管methods,返回一个对象
		// methods:mapActions([
		//  'add'
		//])

		//mapMutations 接管methods,跳过actions找mutations
		//methods:mapMutations([
		//    'increment'
		// ])

		// methods: {
		// 	...mapMutations([ //mapMutations 返回来一个对象
		// 		'increment'
		// 	]),
		// 	...mapActions(['add']), //mapActions 返回来一个对象

		// 	show() { //组件内部methods
		// 		...
		// 	}
		// },

		//mapGetters接管computed,返回一个对象
		// computed: mapGetters([
		// 	'count'
		// ]),

		// computed: mapState([
		// 	'count'
		// ]),
		computed: {
			// ...mapState([ //mapState返回一个对象
			// 	'count'
			// ]),
			...mapGetters([
				'count'
			]),

			// count() { //组件内容计算属性
			// 	return this.$store.state.count % 2 === 0 ?
			// 		this.$store.state.count + '偶数' :
			// 		this.$store.state.count + '奇数'
			// }
		}
	}  

状态管理主要是把握store里面的几个成员的交互流程!!!

注意一点在组件中的应用是否要加payload参数,依据methods中使用的那种请求方式(编程式发送请求,传payload参数;如果是mapMutations、mapActions接管methods,在调用处传参)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值