Vuex中this.$store.commit()和this.$store.dispatch()区别说明

 更新时间:2022年04月02日 15:09:27   作者:拐锅  
这篇文章主要介绍了Vuex中this.$store.commit()和this.$store.dispatch()区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

this.$store.commit()和this.$store.dispatch()的区别

两个方法其实很相似,关键在于一个是同步,一个是异步

commit: 同步操作

this.$store.commit('方法名',值) //存储
this.$store.state.'方法名' //取值

dispatch: 异步操作

this.$store.dispatch('方法名',值) //存储
this.$store.getters.'方法名' //取值

当操作行为中含有异步操作,比如向后台发送请求获取数据,就需要使用action的dispatch去完成了,其他使用commit即可.

其他了解

  • commit => mutations, 用来触发同步操作的方法.
  • dispatch => actions, 用来触发异步操作的方法.

在store中注册了mutation和action

在组件中用dispatch调用action,用commit调用mutation

Vuex应用实例this.$store.commit()触发

新建文件夹store,store下

action.js

const actions = {}
export default actions;

getter.js

const getters = {}
export default getters;

mutation-types.js

export const publicSetEvent = 'publicSetEvent';

mutations.js

import {publicSetEvent} from './mutation-types';
const mutations = {
    [publicSetEvent]: (state, json) => {
    // 初始化默认,避免跳转路由时的公用部分显示的相互影响
       state.publicSet = {headTitle: true,headNav: false,sTitle: '头部标题'}
// 是否显示头部title
        state.publicSet.headTitle = json.headTitle || state.publicSet.headTitle;
        // 是否显示头部tabbar切换
        state.publicSet.headNav = json.headNav || state.publicSet.headNav;
        // 头部显示的标题文字
        state.publicSet.sTitle = json.sTitle || state.publicSet.sTitle;
        // tabbar的标题文字及待办badge数字
        state.publicSet.navList = json.navList || state.publicSet.navList;
    }
}
export default mutations;

index.js

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations';
import getters from './getters';
import actions from './actions';
Vue.use(Vuex);
const state = {
    publicSet: {//设置公共头
        headTitle: true,
        headNav: false,
        sTitle: '头部标题'
    }
}
const store = new Vuex.Store({
    state,
    getters,
    mutations,
    actions
});
export default store;

头部公共组件components文件夹下

v-header.vue

<template>
  <div class="v-header">
    <vTitle v-if="publicSet.headTitle" :stitle="publicSet.sTitle"></vTitle>
  </div>
</template>
<script>
import vTitle from './v-title';
import {mapState} from 'vuex';
export default{
   name:'v-header',
   components:{vTitle},
   data(){
    return{
      
    }
   },
   computed: {
       ...mapState(['publicSet'])
   }
}
</script>

v-title.vue

<template>
  <div class="v-title">
      <XHeader :left-options="{backText:''}" :title="stitle"></XHeader>
  </div>
</template>
<script>
import { XHeader } from 'vux'
export default{
  name:'v-title',
  props:['stitle'],
  components:{XHeader},
  data (){
      return {
      }
  },
  methods: {
  }
}
</script>
<style lang="less">
</style>

App.vue

<template>
  <div id="app">
    <vHeader></vHeader>
    <router-view/>
  </div>
</template>
<script>
import vHeader from '@/components/header/v-header'
export default {
  name: 'app',
  components:{vHeader}
}
</script>

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import Vuex from 'vuex'
import store from './store'
Vue.use(Vuex)
Vue.config.productionTip = false
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

页面调用index.vue

<template>
    <div class="index">
    </div>
</template>
<script>
export default{
    name:'index',
    data(){
        return{
        }
    },
    created(){
    },
    beforeRouteEnter(to,from,next){
        let option={
          headTitle:true,
      sTitle:'我是新标题'
        }
        console.log(option);
        next(vm=>{
          vm.$store.commit('publicSetEvent',option);
        })
    },
    methods:{
    }    
}
</script>
<style lang="less">
</style>

运行进去index页面就可以看到公共头了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • Vue中Mixin的正确用法详解

    Vue中Mixin的正确用法详解

    众所周知,vue 的 mixins 是个非常灵活,但又很容易带来混乱的 API,Mixins 本该是一种强大的重用代码的手段,但使用之后往往带来更多的混乱,代码变得不易维护,本文就详细介绍Vue Mixin的正确用法,需要的朋友可以参考下
    2023-06-06
  • vue实现输入一位数字转汉字功能

    vue实现输入一位数字转汉字功能

    这篇文章主要介绍了vue实现输入一位数字转汉字功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • vue指令v-html使用过滤器filters功能实例

    vue指令v-html使用过滤器filters功能实例

    在本篇文章里我们给大家整理的是关于vue指令v-html使用过滤器filters功能的实例内容,需要的朋友们学习下。
    2019-10-10
  • 关于vue3.0中的this.$router.replace({ path: ''/''})刷新无效果问题

    关于vue3.0中的this.$router.replace({ path: ''/''})刷新无效果问题

    这篇文章主要介绍了关于vue3.0中的this.$router.replace({ path: '/'})刷新无效果问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Vue ECharts直角坐标系配置详细讲解

    Vue ECharts直角坐标系配置详细讲解

    数据的重要性我们大家都知道,就算再小的项目中都可能使用几个图表展示,我最近在做项目的过程中也是需要用到图表,最后选择了echarts图表库
    2022-12-12
  • vuex数据持久化的两种实现方案

    vuex数据持久化的两种实现方案

    在vuex的时候刷新以后里面存储的state就会被浏览器释放掉,因为我们的state都是存储在内存中的,所以一刷新页面就会把state中的数据重置,这就涉及到vue数据持久化的问题,这篇文章主要给大家介绍了关于vuex数据持久化的两种实现方案,需要的朋友可以参考下
    2021-07-07
  • vue.js 实现a标签href里添加参数

    vue.js 实现a标签href里添加参数

    今天小编就为大家分享一篇vue.js 实现a标签href里添加参数,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Vue的transition-group与Virtual Dom Diff算法的使用

    Vue的transition-group与Virtual Dom Diff算法的使用

    这篇文章主要介绍了Vue的transition-group与Virtual Dom Diff算法的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • 解决vue change阻止默认事件问题

    解决vue change阻止默认事件问题

    这篇文章主要介绍了vue change阻止默认事件问题,使用事件 @click.stop.native.prevent 解决 (使用@click.stop 或者 @click.prevent都无效,直接报错还阻止不了事件),需要的朋友可以参考下
    2022-01-01
  • vue中数据不响应的问题及解决

    vue中数据不响应的问题及解决

    这篇文章主要介绍了vue中数据不响应的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09

最新评论