15分钟学会vuex。State、 Getter、Mutation 、Action简述

本文详细介绍了Vuex在Vue项目中的作用,包括它如何用于组件间的数据共享。文章通过实例演示了State、Getter、Mutation和Action的用法,并解释了它们各自的功能:State用于定义和暴露共享变量,Mutation专门存放修改State的同步函数,Action处理异步请求并在适当时候调用Mutation,Getter则作为State的计算属性提供复杂的数据获取。此外,还展示了如何在Vue组件中使用这些概念。

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

vuex作用:组件直接互相传值,存共享数据 (官方回答请看:Vuex 是什么? | Vuex

vuex中的五个属性:State、 Getter、Mutation 、Action、 Module 

目录

State:定义变量,暴漏出去,让所有页面都可以拿到值

mutations:专门存放修改store的函数。mutations有2个参数,第一个默认为state,接下来的为自定义参数

Action:异步请求。有2个参数,第一个默认为context(整个store的配置,可以打印一下),接下来的为自定义参数

Getter:获取、计算state。属于state的计算属性,可以理解类似于Vue中computed


State:定义变量,暴漏出去,让所有页面都可以拿到值

安装:npm install vuex --save

1、创建store文件夹,创建index.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const state = {
	count:100
}
export default new Vuex.Store({
	state
})

2、main.js中引用 (这里是为了防止在各个组件中引用,因为main.js中,有我们的new Vue 实例啊!)


import Vue from 'vue';
import Vuex from 'vuex'; //引用的
import App from './App';
import router from './router';
import store from './store/index.js';//引用的

Vue.use(Vuex);

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
});

3、页面中使用

<template>
  <div>
    {{$store.state.count}}  // 会输出==>  100
  </div>
</template>

mutations:专门存放修改store的函数。mutations有2个参数,第一个默认为state,接下来的为自定义参数

1、在之前创建的store文件夹中的index.js中创建mutations

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const state = {
	count: 100
}
const mutations = { //创建
    mutationsAdd(state, n = 0) {
        return (state.count += n)
    },
    mutationsReduce(state, n = 0) {
        return (state.count -= n)
    }
}
export default new Vuex.Store({
	state,
	mutations //导出
})

2、在页面中使用 (点击button就可以加减store了)

<template>
  <div>
    {{$store.state.count}}
    <button @click="add">+</button> <!--添加事件-->
    <button @click="reduce">-</button>
  </div>
</template>
<script>
export default {
  methods:{
    add(){
      this.$store.commit('mutationsAdd',11) //commit是调用mutation的方法
    },
    reduce(){
      this.$store.commit('mutationsReduce',11)
    }
  }
}
</script>
  

Action:异步请求。有2个参数,第一个默认为store(整个store的配置,可以打印一下),接下来的为自定义参数

1、在之前创建的store文件夹中的index.js中创建action

tip:操作页面的时候,如果有异步操作,那么先在页面中调用action,action中再调用mutation,使用mutation中的方法修改state。

vuex是一个很有规律的东西,每个属性只负责自己的那点东西,互不干涉。想要修改state就只能通过过mutation修改。

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const state = {
	count: 100
}
const mutations = {
    mutationsAdd(state, n = 0) {
        return (state.count += n)
    },
    mutationsReduce(state, n = 0) {
        return (state.count -= n)
    }
}
const actions = { //新创建的
    actionsAdd(store, n = 0) {
        console.log(context)
        return store.commit('mutationsAdd', n)
    },
    actionsReduce({ commit }, n = 0) {
        return commit('mutationsReduce', n)
    }
}
export default new Vuex.Store({
	state,
	mutations,
	actions //导出
})

2、在页面中使用 (点击button就可以加减store了)

<template>
  <div>
    {{$store.state.count}}
    <button @click="add">+</button> <!--添加事件-->
    <button @click="reduce">-</button>
  </div>
</template>
<script>
export default {
  methods:{
    add(){
      // this.$store.commit('mutationsAdd',11)  这时之前调用mutation中的方法修改state
      this.$store.dispatch('actionsAdd',11)  //调用action中的方法用dispatch
    },
    reduce(){
      // this.$store.commit('mutationsReduce',11)
      this.$store.dispatch('actionsReduce',11)
    }
  }
}
</script>

Getter:获取、计算state。属于state的计算属性,可以理解类似于Vue中computed

1、在之前创建的store文件夹中的index.js中创建getter

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

const state = {
	count: 100,
	todos: [
		{ id: 1, text: '水果类'},
		{ id: 2, text: '苹果' },
	]
}
const mutations = {
    mutationsAdd(state, n = 0) {
        return (state.count += n)
    },
    mutationsReduce(state, n = 0) {
        return (state.count -= n)
    }
}
const actions = {
    actionsAdd(store, n = 0) {
        console.log(store)
        return store.commit('mutationsAdd', n)
    },
    actionsReduce({ commit }, n = 0) {
        return commit('mutationsReduce', n)
    }
}
const getters = { //新增的getter
    Todos: state => {//通过方法访问
		return state.todos.filter(todo => todo )
	},
	TodosCount: (state, getters) => {//通过属性访问
		return getters.doneTodos.length
	}
}
export default new Vuex.Store({
	state,
	mutations,
	actions,
	getters //导出
})

2、页面中使用

<template>
  <div>
    <P>通过方法访问:{{todos}}</p>
    通过属性访问:{{doneTodosCount}}
  </div>
</template>
<script>
export default {
  computed:{
    todos:function() {  //通过方法访问
      return this.$store.getters.Todos;
    },
    doneTodosCount () { //通过属性访问
      return this.$store.getters.TodosCount
	}
  },
}
</script>
  

自己一个字一个字码的,看懂的求赞。转走的放我链接

如果有写错的地方,求指导

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值