欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vuex 進(jìn)階之模塊化組織詳解

 更新時(shí)間:2018年01月12日 11:42:45   作者:劉飛_007  
這篇文章主要介紹了Vuex 進(jìn)階之模塊化組織詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

上上篇:Vuex 入門

上一篇:Vuex 提升

自制vuex LOGO

前兩篇講解了一下 Vuex 的基本使用方法,可是在實(shí)際項(xiàng)目中那么寫肯定是不合理的,如果組件太多,不可能把所有組件的數(shù)據(jù)都放到一個(gè) store.js 中的,所以就需要模塊化的組織 Vuex,首先看一下 項(xiàng)目結(jié)構(gòu)

 

項(xiàng)目結(jié)構(gòu)

一、首先執(zhí)行以下命令:

vue init webpack-simple vuex-demo
cd vuex-demo
npm install
npm install vuex -S
npm run dev

二、按照上圖結(jié)構(gòu)創(chuàng)建文件目錄

Vuex 模塊化目錄

三、編寫文件

我們就延用上兩篇文章中的例子。先說一個(gè)各個(gè)文件的作用

types.js 內(nèi)定義常量,使用常量替代 mutation 事件類型

user.js 內(nèi)寫該 user 組件內(nèi)用到的 state 、 getters 、 actions 和 mutations,并最后統(tǒng)一導(dǎo)出(類似上個(gè)例子中的 store.js )

getters.js 內(nèi)寫原來的 getters ,用來獲取屬性

actions.js 內(nèi)寫原來的 actions ,就是要執(zhí)行的動(dòng)作,如流程的判斷、異步請(qǐng)求

index.js 是用來組裝 actions.js 、 getters.js 、user.js 的,然后進(jìn)行統(tǒng)一的導(dǎo)出

1. 在 main.js 中導(dǎo)入 index.js 文件并注冊(cè)

import Vue from 'vue'
import App from './App.vue'
import store from './store/index.js'

new Vue({
 store,
 el: '#app',
 render: h => h(App)
})

2. 在 types.js 內(nèi)定義 常量 并導(dǎo)出,默認(rèn)全部大寫

// 定義類型常量,默認(rèn)全部大寫
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'

export default {
  INCREMENT,
  DECREMENT
}

注意:把這些常量放在單獨(dú)的文件中可以讓你的代碼合作者對(duì)整個(gè) app 包含的 mutation 一目了然。用不用常量取決于你——在需要多人協(xié)作的大型項(xiàng)目中,這會(huì)很有幫助。但如果你不喜歡,你完全可以不這樣做。

3. user.js 內(nèi)寫該 user 組件內(nèi)用到的 state 、 getters 、 actions 和 mutations

// 導(dǎo)入 types.js 文件
import types from "./../types";

const state ={
  count:5
}

// 定義 getters
var getters ={
  count(state){
    return state.count
  }
}

const actions ={
  increment({ commit, state }){
    // 此處提交的事件與下方 mutations 中的 types.INCREMENT 對(duì)應(yīng),與原來 commit('increment') 的原理相同,只是把類型名換成了常量
    commit(types.INCREMENT)
  },
  decrement({commit,state}){
    if (state.count>10) {
      // 此處提交的事件與下方 mutations 中的 types.DECREMENT 對(duì)應(yīng)
      commit(types.DECREMENT)
    }
  }
}

const mutations ={
  // 此處的事件為上方 actions 中的 commit(types.INCREMENT)
  [types.INCREMENT](state){
    state.count++
  },
  // 此處的事件為上方 actions 中的 commit(types.DECREMENT)
  [types.DECREMENT](state){
    state.count--
  }
}
// 最后統(tǒng)一導(dǎo)出
export default {
  state,
  getters,
  actions,
  mutations
}

注意:上方 mutations 中的 [types.INCREMENT] 寫法,因?yàn)?types.INCREMENT 是一個(gè)對(duì)象,所以不能直接當(dāng)做一個(gè)函數(shù)名來寫,需要用到 ES2015 風(fēng)格的計(jì)算屬性命名功能來使用一個(gè)常量作為函數(shù)名,方能正常使用,原來的寫法為:

const mutations ={
  increment(state){
    state.count ++;
  }
}

4. getters.js 內(nèi)寫原來的判斷奇偶數(shù)方法

// 因?yàn)閿?shù)據(jù)從 user.js 中獲取,所以需要引入該文件
import user from './modules/user'

const getters = {
  isEvenOrOdd(state){
    // 注意數(shù)據(jù)是從 user.js 中獲取的,所以寫成 user.state.count
    return user.state.count % 2 == 0 ? "偶數(shù)" : "奇數(shù)"
  }
}
// 并導(dǎo)出
export default getters;

5. actions.js 內(nèi)寫原來的異步操作

// 異步操作中需要用到 increment 方法,所以需要導(dǎo)入 types.js 文件
import types from './types'
const actions= {
  incrementAsync({ commit, state }) {
    // 異步操作
    var p = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve()
      }, 3000);
    });
    p.then(() => {
      commit(types.INCREMENT);
    }).catch(() => {
      console.log('異步操作');
    })
  }
}
// 最后導(dǎo)出
export default actions;

6. 在 index.js 中組裝 actions.js 、 getters.js 、user.js 的,然后統(tǒng)一導(dǎo)出

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import getters from './getters'
import actions from './actions'
import users from './modules/user'
// 導(dǎo)出 store 對(duì)象
export default new Vuex.Store({
  getters,
  actions,
  modules:{
    users
  }
})

注意:在導(dǎo)出 store 對(duì)象時(shí),因?yàn)?getters 和 actions 在 vuex 的核心概念中有默認(rèn),可以直接寫入。但是 users 不是默認(rèn)的,所以用到 vuex 中的 modules 對(duì)象進(jìn)行導(dǎo)出

核心概念

7. Vue.app 文件不作任何修改

<template>
 <div id="app">
  <button @click="increment">增加</button>
  <button @click="decrement">減少</button>
  <button @click="incrementAsync">延時(shí)增加</button>
  <p>{{count}}</p>
  <p>{{isEvenOrOdd}}</p>
 </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
export default {
 name: 'app',
 data () {
  return {
   msg: 'Welcome to Your Vue.js App'
  }
 },
 computed:mapGetters([
  'count',
  'isEvenOrOdd'
 ]),
 methods:mapActions([
  'increment',
  'decrement',
  'incrementAsync'
 ])
}
</script>

最后,驚心動(dòng)魄的時(shí)候到了,我這費(fèi)半天勁的東西到底能不能跑起來

vuex模塊化.gif

對(duì)于新手們來說,光是看一次可能很難理解這個(gè)過程,還是要親自多試一試的,以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論