zoukankan      html  css  js  c++  java
  • vue-element-admin中如何vuex的使用

    vue-element-admin中把store给model化了,就是说分成a/b/c/d...的很多模块,每个模块都有自己的state/mutation/getter/action,在大的项目中,这么做的效果还是显而易见的,因为单一的状态树种,随着项目的不断迭代,会有很多状态和mutation放入同一个state/mutation对象中,难免会使对象臃肿不堪。

    一:首先要创建一个自己的store模块,在store/modules中创建一个my.js,并在其中书写自己的state/mutation/action

    const my = {
      state: {
        ceshi: 1
      },
     
      mutations: {
        CESHI_NUM: (state, data) => {
          state.ceshi += data
        },
      },
     
      actions: {
        ceshi({ commit, state }, data) {
          commit('CESHI_NUM', data)
        },
      }
    }
     
    export default my

    二:在store的index中注册my.js

    import my from './modules/my'
     
    Vue.use(Vuex)
     
    const store = new Vuex.Store({
      modules: {
        my
      }
    })

    三:在组件中调用和改变状态树

    <template>
      <div class="">
        <el-button type="primary" icon="el-icon-search" @click="change_vuex">测试vuex</el-button>
        <el-button type="primary" icon="el-icon-search" @click="change_vuex_ansnc">异步测试vuex</el-button>
        <div>原stor中注册的为:{{ ceshia }}</div>
      </div>
    </template>
    <script>
    import store from '../../../store'
    export default {
      mounted() {
        
      },
      data() {
        return {
          
        }
      },
      computed: {
        ceshia() {
          return store.state.my.ceshi
        }
      },
      methods: {
        change_vuex() {
          store.commit('CESHI_NUM',10)
        },
        change_vuex_ansnc() {
          store.dispatch('ceshi', 10)
        },
      }
    }
    </script>

    参考---https://blog.csdn.net/wangle_style/article/details/90287742

  • 相关阅读:
    [bzoj2038] [2009国家集训队]小Z的袜子
    浅谈莫队
    [bzoj2754] [SCOI2012]喵星球上的点名
    [bzoj3676] [APIO2014]回文串
    [bzoj5472] 数列
    [bzoj5457] 城市
    [bzoj1023] [SHOI2008]cactus仙人掌图
    [bzoj2125] 最短路
    [bzoj5473] 仙人掌
    读《深入理解Elasticsearch》点滴-查询评分
  • 原文地址:https://www.cnblogs.com/pwindy/p/14657631.html
Copyright © 2011-2022 走看看