vue-store mode
vueX
props
$emit
vuex use the steps
// 引入Vue、Vuex三方件
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
user,
search
},
getters
})
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
vuex basic concepts:
state: a single state tree
mapState: Helper
Role: When a component needs to obtain multiple states when these states are declared to calculate the property will be some duplication and redundancy.
import { mapState } from 'vuex'
export default {
// ...
computed: {
localComputed () { /* ... */ },
...mapState({
// 箭头函数可使代码更简练
count: state => state.count,
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
})
}
}
getter
Action: state attribute of similar calculations, for secondary processing state, eg: filter
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)
}
}
})
获取:this.$store.getters.doneTodosCount
mapGetters: Helper
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'doneTodosCount',
'anotherGetter',
// ...
])
}
}
// mapGetters取别名
mapGetters({
// 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
doneCount: 'doneTodosCount'
})
Mutation (synchronous submission)
Role: The only way to change the state status value is to submit a mutation
Note: mutation must be synchronized function
const store = new Vuex.Store({
state: {
count: 1
},
mutations: {
increment (state,payload) {
// 变更状态
state.count += payload.amount
}
}
})
// 提交方式(10,称为载荷)
store.commit('increment', {
amount:10
})
mapMutation
Action: The assembly methods mapped store.commit call (at the root node needs to store injection)
import { mapMutations } from 'vuex'
export default {
// ...
methods: {
...mapMutations([
'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
// `mapMutations` 也支持载荷:
'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
]),
...mapMutations({
add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
})
}
}
action (asynchronous submission)
Role: asynchronous submit mutation, not directly modify the state status
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
},
actions: {
increment (context) {
context.commit('increment')
}
}
})
dispatch (distribution)
Role: asynchronous load submit action
store.dispatch('incrementAsync', {
amount: 10
})
Modules
const moduleA = {
state: { count: 0 },
mutations: {
increment (state) {
// 这里的 `state` 对象是模块的局部状态
state.count++
}
},
getters: {
doubleCount (state) {
return state.count * 2
}
}
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})
store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态
Namespaces