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

vuex 項目結構目錄及一些簡單配置介紹

 更新時間:2018年04月08日 08:59:35   投稿:mrr  
這篇文章主要介紹了vuex 項目結構目錄及一些簡單配置,需要的朋友可以參考下

首先先正經的來一段官網的"忠告":

vuex需要遵守的規(guī)則:

一、應用層級的狀態(tài)應該集中到單個 store 對象中。

二、提交 mutation 是更改狀態(tài)的唯一方法,并且這個過程是同步的。

三、異步邏輯都應該封裝到 action 里面。

文件目錄結構

文件之間的關系:

store文件夾 - 存放vuex的系列文件

store.js - 引入vuex,設置state狀態(tài)數據,引入getter、mutation和action

getter.js - 獲取store內的狀態(tài)

mutation.js - 更改store中狀態(tài)用的函數的存儲之地

action.js - 提交mutation以達到委婉地修改state狀態(tài),可異步操作

簡單而又普通的寫法

store.js文件:

import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
const state = {
 a: '初始值',
 b: 'balabala...'
}
export default new Vuex.Store({
  state,
  actions,
  mutations
})

main.js文件中(從根組件注入store,就像注入router一樣):

通過在根實例中注冊 store 選項,該 store 實例會注入到根組件下的所有子組件中,且子組件能通過 this.$store 訪問到。

import store from './store/index'
new Vue({
 el: '#app',
 router,
 store,
 ...
})

Getter.js 的簡單配置( store 的計算屬性,接受state為參數)

export default {
  doneTodos: state = >{
   return state.todos.filter(todo = >todo.done)
  }
}

獲取(某組件的計算屬性內部):

computed: {
 doneTodosCount () { 
  return this.$store.getters.doneTodosCount 
 }
}

可傳參 的getter屬性的簡單配置

export default{

 getTodoById: (state) => (id) => { 
  return state.todos.find(todo => todo.id === id) 
 }
}

獲?。?組件的計算屬性內部 ):

computed: {
 getTodoById() { 
  return this.$store.getters.getTodoById(‘參數')
 }
}

mutation.js簡單配置:

export default {
  increment(state) {
   //變更狀態(tài)
   state.count++
  }
}

觸發(fā)(組件中)

this.$store.commit(state,payload)
actions.js簡單配置:
export default{
 action (context) {
 //異步操作
  setTimeout(()=>{
   //變更狀態(tài)
   context.commit('mutationFunName',value)
  })
 }
}

觸發(fā)(組件的)

this.$store.dispatch('mutationFunctionName')
2018-04-07 18:13:34

總結

以上所述是小編給大家介紹的vuex 項目結構目錄及一些簡單配置介紹,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

最新評論