vuex-データマネージャ
9559 ワード
vuex-データマネージャ
「vuexは、Vue用の状態管理モードであり、集中型ストレージ管理アプリケーションのすべてのコンポーネントの状態を採用しています.」
vuexの応用コアは倉庫(store)であり、管理は状態であり、状態は共用の属性またはデータであり、vuexは共用の属性を管理し、vuexの特徴は、第1の状態記憶は応答式であり、第2の点はstoreの状態を直接変更することができず、storeの状態を変える唯一の方法は表示のコミットmutationsである.
各アプリケーションにはstoreインスタンスが1つしか含まれていません
インストール方法:
npm install vuex –save
Stateについて
state:ステータスオブジェクト、共有値をステータスと呼ぶ
まずstoreをインポートする必要があります.js
import Vue from ‘vue’ import Vuex from ‘vuex’
Vue.use(Vuex);
stateの値にアクセスする方法:計算属性は1つの状態を返す:Store.state.count storeオプション を通過
「vuexは、Vue用の状態管理モードであり、集中型ストレージ管理アプリケーションのすべてのコンポーネントの状態を採用しています.」
vuexの応用コアは倉庫(store)であり、管理は状態であり、状態は共用の属性またはデータであり、vuexは共用の属性を管理し、vuexの特徴は、第1の状態記憶は応答式であり、第2の点はstoreの状態を直接変更することができず、storeの状態を変える唯一の方法は表示のコミットmutationsである.
各アプリケーションにはstoreインスタンスが1つしか含まれていません
インストール方法:
npm install vuex –save
Stateについて
state:ステータスオブジェクト、共有値をステータスと呼ぶ
まずstoreをインポートする必要があります.js
import Vue from ‘vue’ import Vuex from ‘vuex’
Vue.use(Vuex);
store.js
の内容:const store = new Vuex.Store({
state: {
num: 0
},
mutations: {
increment (num) {
state.num++
}
}
})
stateの値にアクセスする方法:
const app = new Vue({
el: '#app',
store, // store “store” , store
components: { Counter },
template: `class="app"><counter>counter>div>`
})
this.$store.state.count
const Counter = {
template: `{{ count }}</div>`,
computed: {
count () {
return this.$store.state.count
}
}
}
mapState
, , mapState
import {mapState} from 'vuex'
export default{
....
computed : mapState({
//
count : state => state.count,
// count state => state.count
countAlias: 'count',
// this ,
countPlusLocalState(state) {
return state.count + this.localCount
}
})
}
マッピングされた の とstateのサブノード が じである 、mapStateに を すことができます.computed: mapState([
// this.count store.state.count
'count'
])
Gettersについてstate
からいくつかの を させる がある 、 えばリストをフィルタしてカウントする
まず、これらは によって ることができるが、 によって ると、コードの が し、vuexではgetter
を することができ、stateを のパラメータとして け れることができる.const store = new Vuex.Store({
state: {
todos: [
{ id: 1, text: '...', done: true },
{ id: 2, text: '...', done: false }
]
},
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
})
store.getters.doneTodos
で すると、2 のパラメータは のgettersであってもよいmapGetters
を りることもできますimport { mapGetters } from 'vuex'
export default {
// ...
computed: {
// getters computed
...mapGetters([
'doneTodosCount',
'anotherGetter',
// ...
])
}
}
...
は、 を してstateの を するとともに、getterのフィルタ を び す に です. は い です.computed: {
// state
...mapState(['count']),
// getter
count() {
return this.$store.getters.doneTodos;
}
}
のも に けますimport { mapGetters } from 'vuex'
computed: {
// state
...mapState(['count']),
// getter
...mapGetters({'doneTodos'})
}
Mutation
Vuexのstoreのステータスを する の はコミットです.コンポーネントではMutationsのメソッドを び すことはできません.commit
でのみできます.たとえば、 のようにします.const store = new Vuex.Store({
state: {
count: 1
},
mutations: {
increment (state) {
state.count++
}
}
})
び し :store.commit('increment')
もちろん、 のパラメータを すこともできます. mutations: {
increment (state, n) {
state.count += n;
}
}
store.commit('increment', 10)
mutation
は でなければなりません.リターン の の は できないからです.
のstore.commit('increment')
はコンポーネントに くのも ですimport { mapMutations } from 'vuex'
export default {
// ...
methods: {
...mapMutations([
'increment' // this.increment() this.$store.commit('increment')
]),
...mapMutations({
add: 'increment' // this.add() this.$store.commit('increment')
})
}
}
のようにすることもできます.methods : mapMutations(['add', 'reduce'])
Action
Actionはmutationに ていますが、 います. Actionは、ステータス を するのではなく、mutationをコミットする.アクションは、 の を むことができる.actions: {
addActions(context) {
context.commit('add', 100)
},
reduceAction({commit}) {
commit('reduce');
}
}
import {mapActions, mapMutations} from 'vuex'
methods: {
...mapMutations(['add', 'reduce']),
//this.$store.dispatch('addAction')
...mapActions(['addAction', 'reduceAction'])
}
:actions: {
addActions(context) {
context.commit('add', 100);
setTimeout(() => {context.commit('reduce')}, 4000);
},
reduceAction({commit}) {
commit('reduce');
}
}
:
アクションはstore.dispatch('increment')
コンポーネントでは、 の を してもよいし、 methods: {
...mapActions([
'increment' // this.increment() this.$store.dispatch('increment')
]),
...mapActions({
add: 'increment' // this.add() this.$store.dispatch('increment')
})
}
モジュールグループconst moduleA = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleB = {
state: { ... },
mutations: { ... },
actions: { ... }
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a // -> moduleA
store.state.b // -> moduleB