vuex應用實例-this.$store.commit()觸發

新建文件夾store,store下:vue

action.jsvuex

const actions = {}
export default actions;

getter.jsjson

const getters = {}
export default getters;

mutation-types.jsapp

export const publicSetEvent = 'publicSetEvent';

mutations.jsless

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.jsspa

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文件夾下code

v-header.vuecomponent

<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.vuerouter

<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.vueblog

<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頁面就能夠看到公共頭了

相關文章
相關標籤/搜索