Vuex状态管理级辅助函数的使用,只需要你花20分钟认真了解一遍,在新的小白以后都很清晰了

Vuex你懂这些就够了

先说为啥用它,举个例子 ,这里对你了解Vuex很重要,

在我们平时的项目中,比如好几个页面都用到同一个数据,并且这个数据是在一个页面改变,其他页面也需要变化,此时,我们使用vuex
就很方便了,
直接上代码,
相信来看vuex的同学,搭建一个vue项目那不用说了吧,
先看目录结构
在这里插入图片描述
页面展示如下,下面会给出每个页面的代码,加上解释
HelloWorld组件不需要,自己删除即可,初始化项目后,如果新手,对着目录建立组件,代码直接粘贴进去,运行起来,对着每个方法点击,理一遍,你就会很清晰了 ,
在这里插入图片描述
现在的需求是 头部组件和脚步组件都使用一个一个数字,我们在身体组件中点击减少,头部脚组件都要变化 点击赋值 actions按钮都要变化,这样这个值就可以在整个项目所有页面都可以使用,

Home.vue
引入组件,大家都懂,没有啥好说的

<template>
  <div class="home">
    <HelloWorld msg="状态管理辅以及辅助函数"/>
    <Header />
    <Count />
    <Footer />
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import Count from '@/components/Count.vue'

export default {
  name: 'Home',
  components: {
    HelloWorld,
    Header,
    Count,
    Footer,
    
  }
}
</script>

再来看store下面的index.js
我们头部脚部用的数据就是count

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

Vue.use(Vuex)


const store = new Vuex.Store({
  state: {
    count: 0,
    name:"this store name"
  },
  mutations: {
    //赋值
    changenum(state,val) {
      state.count = val;
    },
    //每次减1
    asignnum(state,val){
      state.count--;
    },
   //
    actionsFn(state,val){
     
        state.count++;
    }
  },
  actions: {
    increment (context,payload) {
      console.log(payload)
      //在调用 mutations中的 actionsFn
      context.commit('actionsFn',payload)
    }
    
  },
  modules: {

  }
})

export default store

再来看内容组件师是怎么操作 的
Count.vue

<template>
  <div class="Count">
      <h3>我是身体组件</h3>
    <button class="btn" @click="add1">赋值</button>
    <button class="btn" @click="asignnum">减少</button>
     <button class="btn" @click="chanhenum">actions</button>
  </div>
</template>
<script>
 import { mapState, mapMutations } from 'vuex'
export default {
  name: 'Count',
  props: {
   
  },
  data () {
      return {
          
      }
  },
  methods: {
       //方法二    
          //点击赋值按钮的时候  需要传参 , this.changenum 要和下面  mapMutations中的changenum对应
            add1(){
                 this.changenum(20);
            },
           
          
           //调用 store中的increment 方法
           //这个师通过操作 actions 再来改变  mutations 中的方法  actionsFn
           chanhenum(){
              this.$store.dispatch('increment',1000);
           }, 
			//这样是使用辅助函数的方式
			//下面就是store中我们需要操作的方法,注意名字对应上
         ...mapMutations([
                'changenum',
                'asignnum'
            ])
            
		//方法一    
		//这是我们传统的写法 方法少的时候这样写师可以的 当我们点击 赋值按钮的时候,调用 store中  mutations中的
		//changenum的这个方法,把传递过来的20赋值给 store中的 count
        // add1(){
        // //点击这个方法 改变store中的数据,这个数据头部脚步公用
        //     this.$store.commit('changenum', 20)
        // },
        
        // asignnum(){
        //      this.$store.commit('asignnum', 520)
        // }
  },
  created () {
      
  },
  mounted () {
     
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  lang="scss">
.Count{
    width: 100%;
    height: 200px;
    background: salmon;
}
.btn{
    background: skyblue;
    border: 0;
    margin-left: 10px;
}
</style>

再来看脚步组件Footer.vue

<template>
  <div class="hello">
     我是脚步组件
     {{sum}}
  </div>
</template>

<script>
export default {
  name: 'Footer',
  props: {
    msg: String
  },
  //计算属性 来获取store中count  这是传统的写法 如果我们需要使用的数据很多 ,那不能写N个这样的方法吧
  //下面**header.vue**中就是使用辅助函数的方式来获取的
    computed:{
        sum:function(){
            return this.$store.state.count;
        },
      
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.hello{
    width: 100%;
    height: 50px;
    background: #ccc;
}
</style>

Header.vue

<template>
  <div class="hello">
       <p>我是头部组件</p><br/>
       {{count}} <br/>
       <br/>
       {{name}}
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: 'Header',
  props: {
    msg: String
  },
   data () {
      return {
         
      }
  },
//这样不是store中有多少数据,我们都只需往下面的mapState中写入即可,就已name 为例 
//你可以仔细查看store下面的index.js中state下数据
    computed: {
        ...mapState(["count","name"])
    },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.hello{
    width: 100%;
    background: #ee5600;
}
</style>

看懂这个已经能满足平时的需求了,至于什么vuex进阶,模块啥的,说实话,一般的小项目真心用不上,我不喜欢杀鸡用宰牛刀。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: Vuex提供了一些辅助函数(mapState、mapActions、mapMutations),可以帮助我们在Vue组件中更方便地使用Vuex状态管理功能。\[1\]这些辅助函数可以通过将Vuex.store中的属性映射到Vue实例中,使我们能够在Vue实例中直接访问Vuex.store中的属性,从而方便地操作Vuex.store。\[2\]使用辅助函数的方法是在Vue组件的computed属性和methods属性中使用这些辅助函数。例如,使用mapState辅助函数可以将Vuex.store中的状态映射到Vue组件的computed属性中,使用mapActions辅助函数可以将Vuex.store中的actions映射到Vue组件的methods属性中。\[3\]在使用辅助函数时,可以指定命名空间的路径来访问多个模块中的状态和操作。例如,使用mapState和mapActions辅助函数时,可以通过给定命名空间的路径来指定要映射的模块,并在Vue组件中使用这些映射后的方法。 #### 引用[.reference_title] - *1* *2* [vuex辅助函数](https://blog.csdn.net/weixin_59519449/article/details/122134753)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vuex辅助函数使用方法](https://blog.csdn.net/AkeDuan/article/details/123574487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1登峰造极

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

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

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

打赏作者

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

抵扣说明:

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

余额充值