zoukankan      html  css  js  c++  java
  • Vue状态管理

    1、导出Vuex

    import Vuex from 'vuex'

    2、定义store

    /*状态管理*/
    const store = new Vuex.Store({
      state: {
        headerShow: true//是否显示头部
      },
      mutations: {  //不应直接更改state,应通过mutations下的方法来更改状态
        setHeaderShow(state, newValue) {
          this.state.headerShow = newValue;
        }
      }
    });

    3、将store注入

    new Vue({
      store,//把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
      render: h => h(App)
    }).$mount('#app-box')

    4、store状态更改

    this.$store.commit('setHeaderShow', true);

    5、子组件中获取状态 使用mapState

    import { mapState } from 'vuex'
    
      export default {
        name: 'app',
        components: {
      
        },
        computed: {
          ...mapState({
            headerShow: state => state.headerShow
          })
        },
    }
  • 相关阅读:
    CSS
    人物
    CSS
    CSS
    概念- 工业4.0
    C#正则表达式
    六月定律
    c#中实现登陆窗口(无需隐藏)
    c#中关于String、string,Object、object,Int32、int
    一个快速找第k+1小的算法
  • 原文地址:https://www.cnblogs.com/tangchun/p/7987637.html
Copyright © 2011-2022 走看看