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



 

v-title.vue





 

App.vue



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





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

转载于:https://www.cnblogs.com/leiting/p/9753838.html

你可能感兴趣的:(vuex应用实例-this.$store.commit()触发)