1. 程式人生 > >vuex管理頁面標題

vuex管理頁面標題

log nav ont 標題 ret spa tor console urn

1、在store -> mutation-types.js文件新增常量

export const UPDATE_TITLE = ‘UPDATE_TITLE‘

2、新增文件title.js目錄結構如下 store ->moudules ->title.js

import * as types from ‘../mutation-types‘

//state
export const state = {
    navigationTitle : ‘‘
}

//mutation
export const mutations = {
    [types.UPDATE_TITLE](state,title){
        Object.assign(state, { ...payload })
        console.log(state.navigationTitle)
    }
}

//actions
export const actions = {
    saveTitle({commit, dispatch}, payload) {
        commit(types.UPDATE_TITLE, payload)
    },
}

//getters
export const getters = {
    localTitle: state => {
        return { ...state }
    }
}

3、標題組件

<div>{{localTitle.navigationTitle}}</div>
export default {
    computed:{
            ...mapGetters([
                ‘localTitle‘
            ])
        },
}

4、各個頁面

export default {
    mounted(){
        this.$store.dispatch(‘saveTitle‘,{
            navigationTitle:this.title
        })
    }
}

vuex管理頁面標題