Vuex文件拆分

store文件下目錄

index.ts
state.ts
mutation-types.ts
mutations.ts
getters.ts
actions.ts

文件

index.ts

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import mutations from './mutations'
import actions from './actions'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})

state.ts(唯一數據源,包含全部應用層級的狀態)

/* 優先從本地獲取 */
let user = localStorage['user'];
/* 共享數據 */
const state = {
    name:"測試",
    count:1,
    user: user ? JSON.parse(user) : { name: '個人中心'},
    list: {},
}
export default state;

mutation-types.ts(定義常量暴露出去)

export const SET_ADD_COUNT = 'SET_ADD_COUNT'
export const SET_REMOVE_COUNT = 'SET_REMOVE_COUNT'

mutations.ts(提交 mutation,更改 Vuex 的 state 中的數據狀態,接收state作為第一個參數)

import * as TYPES from './mutation-types'
const mutations = {
    [TYPES.SET_ADD_COUNT](state: { count: number; },data: number){
        if(data){
            state.count += data;
        }else{
            state.count++;
        }
    },
    [TYPES.SET_REMOVE_COUNT](state: { count: number; },data: number){
        if(data){
            state.count -= data;
        }else{
            state.count--;
        }
    }
}
export default mutations;

getters.ts(從store 中的 state 中派生出一些狀態 ,暴露為 store.getters 對象)

const getters = {
    _user:function(state: { user: any; }){
        return state.user;
    },
    _list: function(state: { list: any; }){
        return state.list
    },
    _getCount:function(state: { count: number; }){
        return state.count + 10;
    }
}

export default getters;

actions.ts(Action 提交 mutation,執行異步操作)

import * as TYPES from './mutation-types'

const actions = {
    [TYPES.SET_ADD_COUNT](context: { commit: (arg0: string, arg1: any) => void; },val: any){
        context.commit(TYPES.SET_ADD_COUNT,val);
    },
    [TYPES.SET_REMOVE_COUNT](context: { commit: (arg0: string, arg1: any) => void; },val: any){
        context.commit(TYPES.SET_REMOVE_COUNT,val);
    }
}

export default actions;

頁面引入

<template>
  <div class="home">
    我是從頁面直接獲取的值:{{this.$store.state}}
    <p>--------------------------------------</p>
    我是從getters中獲取的值:{{$store.getters}}
    <p>--------------------------------------</p>
    我是從mapState中獲取的值:{{message}}
    <p>--------------------------------------</p>
    我是從mapGetters中獲取的值:{{_getCount}}
    <p>--------------------------------------</p>
    <div>
      <button @click="handelAddCount">add count</button>
      <button @click="handelRemoveCount">remove count</button>
    </div>
  </div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
/* mapState、mapGetters、mapActions輔助函數 */
import {mapState,mapGetters,mapActions} from 'vuex'

export default {
  name: 'Home',
  components: {
    HelloWorld
  },
  computed:{
    ...mapState({
      message: state => state.count + 3
    }),
    ...mapGetters(['_getCount']),
  },
  methods:{
    handelAddCount(){
      /* 提交(異步)actions */
      this.$store.dispatch('SET_ADD_COUNT',10);
      /* 提交(同步)mutations */
      // this.$store.commit('SET_ADD_COUNT');
    },
    handelRemoveCount(){
      /* 提交(異步)actions */
      // this.$store.dispatch('SET_REMOVE_COUNT',4);
      /* 提交(同步)mutations */
      // this.$store.commit('SET_REMOVE_COUNT');
      /* 調用 */
      this.SET_REMOVE_COUNT();
    },
    //mapActions 使用方法一
    ...mapActions(['SET_REMOVE_COUNT']),
    //mapActions 使用方法二
    /* ...mapActions({
        SET_REMOVE_COUNT: "SET_REMOVE_COUNT"
    }) */
  }
}
</script>

_20200401140837.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 安裝 npm npm install vuex --save 在一個模塊化的打包系統中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 2,963評論 0 7
  • vuex是一個狀態管理模式,通過用戶的actions觸發事件,然后通過mutations去更改數據(你也可以說狀態...
    Ming_Hu閱讀 2,039評論 3 3
  • 上一章總結了 Vuex 的框架原理,這一章我們將從 Vuex 的入口文件開始,分步驟閱讀和解析源碼。由于 Vuex...
    你的肖同學閱讀 1,807評論 3 16
  • ### store 1. Vue 組件中獲得 Vuex 狀態 ```js //方式一 全局引入單例類 // 創建一...
    蕓豆_6a86閱讀 739評論 0 3
  • 習慣養成很容易,戒掉卻很難!!! 什么是Vuex? Vuex 是一個專為 Vue.js 應用程序開發的狀態管理模式...
    前端又又閱讀 2,810評論 0 1