Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說明
this.$store.commit()和this.$store.dispatch()的區(qū)別
兩個(gè)方法其實(shí)很相似,關(guān)鍵在于一個(gè)是同步,一個(gè)是異步
commit: 同步操作
this.$store.commit('方法名',值) //存儲(chǔ) this.$store.state.'方法名' //取值
dispatch: 異步操作
this.$store.dispatch('方法名',值) //存儲(chǔ) this.$store.getters.'方法名' //取值
當(dāng)操作行為中含有異步操作,比如向后臺(tái)發(fā)送請(qǐng)求獲取數(shù)據(jù),就需要使用action的dispatch去完成了,其他使用commit即可.
其他了解
commit
=>mutations
, 用來觸發(fā)同步操作的方法.dispatch
=>actions
, 用來觸發(fā)異步操作的方法.
在store中注冊(cè)了mutation和action
在組件中用dispatch調(diào)用action,用commit調(diào)用mutation
Vuex應(yīng)用實(shí)例this.$store.commit()觸發(fā)
新建文件夾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) => { // 初始化默認(rèn),避免跳轉(zhuǎn)路由時(shí)的公用部分顯示的相互影響 ? ? ? ?state.publicSet = {headTitle: true,headNav: false,sTitle: '頭部標(biāo)題'} // 是否顯示頭部title ? ? ? ? state.publicSet.headTitle = json.headTitle || state.publicSet.headTitle; ? ? ? ? // 是否顯示頭部tabbar切換 ? ? ? ? state.publicSet.headNav = json.headNav || state.publicSet.headNav; ? ? ? ? // 頭部顯示的標(biāo)題文字 ? ? ? ? state.publicSet.sTitle = json.sTitle || state.publicSet.sTitle; ? ? ? ? // tabbar的標(biāo)題文字及待辦badge數(shù)字 ? ? ? ? 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: {//設(shè)置公共頭 ? ? ? ? headTitle: true, ? ? ? ? headNav: false, ? ? ? ? sTitle: '頭部標(biāo)題' ? ? } } 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/>' })
頁面調(diào)用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:'我是新標(biāo)題' ? ? ? ? } ? ? ? ? console.log(option); ? ? ? ? next(vm=>{ ? ? ? ? ? vm.$store.commit('publicSetEvent',option); ? ? ? ? }) ? ? }, ? ? methods:{ ? ? }? ?? } </script> <style lang="less"> </style>
運(yùn)行進(jìn)去index頁面就可以看到公共頭了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+iview的菜單與頁簽的聯(lián)動(dòng)方式
這篇文章主要介紹了vue+iview的菜單與頁簽的聯(lián)動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬
xlsx是由SheetJS開發(fā)的一個(gè)處理excel文件的npm庫(kù),適用于前端開發(fā)者實(shí)現(xiàn)導(dǎo)入導(dǎo)出excel文件的經(jīng)典需求,這篇文章主要介紹了vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬,需要的朋友可以參考下2023-11-11Vue.use與Vue.prototype的區(qū)別及說明
這篇文章主要介紹了Vue.use與Vue.prototype的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12關(guān)于Echarts餅圖圖例太長(zhǎng)的解決方案
這篇文章主要介紹了關(guān)于Echarts餅圖圖例太長(zhǎng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Vue3 Element-plus el-menu無限級(jí)菜單組件封裝過程
對(duì)于element中提供給我們的el-menu組件最多可以實(shí)現(xiàn)三層嵌套,如果多一層數(shù)據(jù)只能自己通過變量去加一層,如果加了兩層、三層這種往往是行不通的,所以只能進(jìn)行封裝,這篇文章主要介紹了Vue3 Element-plus el-menu無限級(jí)菜單組件封裝,需要的朋友可以參考下2023-04-04vue form check 表單驗(yàn)證的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue form check 表單驗(yàn)證的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決
剛剛創(chuàng)建好vue項(xiàng)目的時(shí)候,運(yùn)行 npm run dev 會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決方法,需要的朋友可以參考下2022-10-10