Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說(shuō)明
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, 用來(lái)觸發(fā)同步操作的方法.dispatch=>actions, 用來(lái)觸發(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/>'
})頁(yè)面調(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頁(yè)面就可以看到公共頭了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
結(jié)合康熙選秀講解vue虛擬列表實(shí)現(xiàn)
這篇文章主要為大家介紹了結(jié)合康熙選秀講解vue虛擬列表的原理使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue向下滾動(dòng)加載更多數(shù)據(jù)scroll案例詳解
這篇文章主要介紹了Vue向下滾動(dòng)加載更多數(shù)據(jù)scroll案例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Vue3+ElementPlus封裝圖片空間組件的門(mén)面實(shí)例
圖片空間是用于管理上傳圖片的工具,可以讓用戶(hù)方便地存儲(chǔ)、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過(guò)圖片空間組件,用戶(hù)能直接在其他模塊(例如商品圖片)中選擇所需圖片2024-09-09
詳解vue-cli 腳手架項(xiàng)目-package.json
本篇文章主要介紹了詳解vue-cli 腳手架項(xiàng)目-package.json,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
詳解Vue如何實(shí)現(xiàn)顏色選擇與調(diào)色板功能
顏色選擇和調(diào)色板是Web開(kāi)發(fā)中常用的功能,Vue作為一個(gè)流行的JavaScript框架,可以方便地實(shí)現(xiàn)顏色選擇和調(diào)色板功能,本文講講如何在Vue中進(jìn)行顏色選擇和調(diào)色板吧2023-06-06
vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式
這篇文章主要介紹了vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue開(kāi)發(fā)中常見(jiàn)的套路和技巧總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue開(kāi)發(fā)中常見(jiàn)的套路和技巧的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳解如何在vue+element-ui的項(xiàng)目中封裝dialog組件
這篇文章主要介紹了詳解如何在vue+element-ui的項(xiàng)目中封裝dialog組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

