新建文件夹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

<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/>'
})

页面调用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页面就可以看到公共头了

vuex应用实例-this.$store.commit()触发的更多相关文章

  1. vuex中的this.$store.commit

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要多 ...

  2. Vue Vuex中的严格模式/实例解析/dispatch/commit /state/getters

    严格模式 import getters from './getters' import mutations from './mutations' import actions from './acti ...

  3. Vue Vuex 严格模式+实例解析+dispatch/commit + state/getter

    1.严格模式 import getters from './getters' import mutations from './mutations' import actions from './ac ...

  4. 解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要 ...

  5. 【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function

    解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function https://www.cnblogs.com/jaso ...

  6. vue vuex 提交 this.$store.commit({type: 'setSelectPro', selectPro: this.productId});

    1.store.commit({'type':'mutation','parameter':'value'}); store.dispatch('action'); 2.获取state保存的值 sto ...

  7. vuex中this.$store.dispatch和this.$store.commit的区别(都是调用vuex中的方法。一个异步一个同步)

    dispatch:含有异步操作,例如向后台提交数据,写法: this.$store.dispatch('action方法名',值) commit:同步操作,写法:this.$store.commit( ...

  8. vuex 使用实例

    官网:https://vuex.vuejs.org/zh/guide/state.html Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 1.vuex解决了组件之间同一状态的共享问题 ...

  9. 在Vuex使用 以及 dispatch和commit来调用mutations的区别

    main.js中 import Vuex from 'vuex' Vue.use(vuex); const store = new Vuex.store({ state: { nickName: &q ...

随机推荐

  1. 随机用户id号,随机密码用户名

    类似新浪微博的用户Id怎么生成呢? 特点:10位随机数,而且是以1开头的 好处:不容易猜出有多少用户 方法一: 目的是生成唯一id.可以用uniqid.uniqid获取一个字符串,循环这个字符串,把每 ...

  2. [svc]tomcat配置文件详解

    Tomcat系列之服务器的安装与配置以及各组件详解 tomcat 配置文件详解 tomcat安全管理规范

  3. JSON 之GSON 解析

    一. 谷歌GSON这个Java类库可以把Java对象转换成JSON,也可以把JSON字符串转换成一个相等的Java对象.Gson支持任意复杂Java对象包括没有源代码的对象. 二.Gson解析Json ...

  4. sass 的使用心得

    //定义颜色 $c55:#; $c22:#; $c33:#; $c99:#; $c77:#; $c00:#; $cff:#fff; $caa:#aaa; $ccc:#ccc; $cf0:#f0f0f0 ...

  5. 用分立元件实现串口通讯TTL/RS232电平转换

    1.计算机串口通信的RS-232电平:用正负电压来表示逻辑状态.逻辑1= = -3V--15V,逻辑0=+3-+15V. 2.单片机串口通信的TTL电平:输出高电平>2.4V,输出低电平< ...

  6. linphone 在am335x的编译过程

    环境变量: export PREFIX=/usr export HOSTTPL=arm-linux-gnueabihf export INSTALLDIR=/home/elinux/linphone/ ...

  7. DRBD 高可用配置详解(转)

    高可用性集群解释:一般是指当集群中有某个节点失效的情况下,其上的任务会自动转移到其他正常的节点上.还指可以将集群中的某节点进行离线维护再上线,该过程并不影响整个集群的运行.今天来做个Heartbeat ...

  8. 140725暑期培训.txt

    1.若须要使用64位int   定义  __64int   类型  %I64d 2.Fibbonacci 数列  採用递归的方法    int  F(int  n)    {        if(n= ...

  9. ThinkPHP 汉字转成多种形式拼音

    模型: <?php namespace Admin\Model; use Think\Model; /** * 汉字转拼音 * @author huangguojin */ class ZHMo ...

  10. PHP高并发的解决方案

    这几天面试,被问到这样一个问题:如何解决大流量的高并发问题.起初不知所措,在查阅相关资料,以及网上大牛们的解答之后,总结出以下几点: 1.服务器,如果同时访问量超过10W的话,需要采用专用服务器来承载 ...