2021-12-10 vue移动端卖座电影项目(十) 使用状态管理模式vuex的state控制选项卡tabbar的显隐,mutation的用法,this.$store.commit()的两个参数

本文介绍了如何在Vue移动端应用中利用Vuex的状态管理模式来控制选项卡Tabbar的显示和隐藏。通过在state中定义showTabbar并引用,结合组件生命周期钩子,实现进入详情页时自动隐藏Tabbar。同时,文章讨论了为何使用mutation进行状态变更,并详细阐述了commit方法的参数用法,即mutation名字和传递的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0.vuex复习

2021-11-19 vue笔记-vuex(一) vuex的安装和使用,vuex核心概念:State,Mutation,Action,Getter

1.引入

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        showTabbar:true
    },
    mutations: {},
    actions: {}
})

main.js

import Vue from "vue"
import router from "./router"
import vueRouter from "vue-router"
import App from "./App.vue"
// 引入store下的index.js
import store from './store/index'
import '@/assets/iconfont/iconfont.css'

Vue.use(vueRouter);
Vue.config.productionTip = false;

new Vue({
    el: "#app",
    router,
    store,
    render: h => h(App)
})
2.在state中存showTabbar后,直接可以引用

App.vue

<tabbar v-show="this.$store.state.showTabbar"></tabbar>
3.在Detail.vue的两个钩子函数中设置控制showTabbar的布尔值

Detail.vue

  beforeMount(){
    //console.log("隐藏tabbar...");
    // Eventbus.$emit('xianshi',false);
    this.$store.state.showTabbar=false;
  },
 mounted(){
...
 },
  beforeDestroy(){
      //console.log("显示tabbar...");
      // Eventbus.$emit('xianshi',true);
    this.$store.state.showTabbar=true;
    }, 
4.效果:实现进入详情页时隐藏tabbar
5.为什么要使用mutation?

vuex通过状态管理实现了与非父子通信一样的效果,
因为谁都可以调用和改动,可能会造成滥用,必须进行监管

6.使用mutation监听
6.1.this.$store.commit()的第一个参数就是mutation的名字

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        //自定义共享状态
        // showTabbar: true
    },
    mutations: {
        listentabbarShow() {
            console.log("修改成显示了...");
        },
        listentabbarHide() {
            console.log("修改成隐藏了...");
        },
    },
    actions: {}
})

Detail.vue

  beforeMount(){
    console.log("隐藏tabbar...");
    // Eventbus.$emit('xianshi',false);
    // this.$store.state.showTabbar=false;
    this.$store.commit('listentabbarHide',false);
  },
 mounted(){
...
 },
  beforeDestroy(){
      console.log("显示tabbar...");
      // Eventbus.$emit('xianshi',true);
    // this.$store.state.showTabbar=true;
    this.$store.commit('listentabbarShow',true);
    }, 
6.2.this.$store.commit()的第二个参数就是一个mutation如listentabbarShow(state,data)的第二个参数data

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        //自定义共享状态
        showTabbar: true
    },
    mutations: { //唯一修改状态的位置
        listentabbarShow(state, data) {
            state.showTabbar = data;
        },
        listentabbarHide(state, data) {
            state.showTabbar = data;
        }
    },
    actions: {}
})

Detail.vue

  beforeMount(){
    console.log("隐藏tabbar...");
    this.$store.commit('listentabbarHide',false);
  },
 mounted(){
...
 },
  beforeDestroy(){
      console.log("显示tabbar...");
    this.$store.commit('listentabbarShow',true);
    }, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端OnTheRun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值