mapGetters相当于激素属性
mapAtion和mapMutation相当于method
-
命名空间方式 名字是:productKnitSales
- 直白方式
this.getGregieOrderId(this.$route.params.id);
// 相当于 this.$store.dispatch("getGregieOrderId", this.$route.params.id)
getGregieOrderId({commit},id){
commit('getGreigeOrderId',id);
},
vuex-2.png
在vuex中提交 mutation 是更改状态的唯一方法,并且这个过程是同步的,异步逻辑都应该封装到 action 里面。对于mutation/action,有一个常见的操作就是传参,也就是官网上说的“提交载荷”。
链接:https://www.jianshu.com/p/0b142955994d
朴实无华的方式
//vuex中的state
const state = {
count: 0
}
export default state;
//vuex中的mutation
const mutations = {
increment: (state,n) => {
//n是从组件中传来的参数
state.count += n;
}
}
export default mutations;
methods: {
add(){
//传参
this.$store.commit('increment',5);
}
}
对象风格提交参数 vue直接提交到mutations
//vuex中的mutation
const mutations = {
decrementa: (state,payload) => {
state.count -= payload.amount;
}
}
export default mutations;
methods: {
reducea(){
//对象风格传参
this.$store.commit({
type: 'decrementa',
amount: 5
});
},
}
朴实无华
/vuex中的action
const actions = {
increment(context,args){
context.commit('increment',args);
}
}
export default actions;
//vuex中的mutation
const mutations = {
increment: (state,n) => {
state.count += n;
}
}
export default mutations;
// vue实例
methods: {
adda(){
//触发action
this.$store.dispatch('increment',5);
}
}
对象风格
//vuex中的action
const actions = {
decrementa(context,payload){
context.commit('decrementa',payload);
}
}
export default actions;
//vuex中的mutation
const mutations = {
decrementa: (state,payload) => {
state.count -= payload.amount;
}
}
export default mutations;
methods: {
reduceb(){
this.$store.dispatch({
type: 'decrementa',
amount: 5
});
}
}
action的异步操作(重点讲下)
action异步
//vuex中的action
const actions = {
asyncMul(context,payload){
//返回promise给触发的store.dispatch
return new Promise((resolve,reject) => {
setTimeout(() => {
context.commit('multiplication',payload);
resolve("async over");
},2000);
});
}
}
export default actions;
//vuex中的mutation
const mutations = {
multiplication(state,payload){
state.count *= payload.amount;
}
}
export default mutations;
methods: {
asyncMul(){
let amount = {
type: 'asyncMul',
amount: 5
}
this.$store.dispatch(amount).then((result) => {
console.log(result);
});
}
}
在另外一个 action 中组合action
action组合 promise方式
//vuex中的action
const actions = {
asyncMul(context,payload){
//返回promise给触发的store.dispatch
return new Promise((resolve,reject) => {
setTimeout(() => {
context.commit('multiplication',payload);
resolve("async over");
},2000);
});
},
actiona({dispatch,commit},payload){
return dispatch('asyncMul',payload).then(() => {
commit('multiplication',payload);
return "async over";
})
}
}
export default actions;
//vuex中的mutation
const mutations = {
multiplication(state,payload){
state.count *= payload.amount;
}
}
export default mutations;
methods: {
actiona(){
let amount = {
type: 'actiona',
amount: 5
}
this.$store.dispatch(amount).then((result) => {
console.log(result);
});
}
}
action组合 asyc await方式
//vuex中的action
const actions = {
asyncMul(context,payload){
//返回promise给触发的store.dispatch
return new Promise((resolve,reject) => {
setTimeout(() => {
context.commit('multiplication',payload);
resolve("async over");
},2000);
});
},
async actionb({dispatch,commit},payload){
await dispatch('asyncMul',payload);
commit('multiplication',payload);
}
}
export default actions;
//vuex中的mutation
const mutations = {
multiplication(state,payload){
state.count *= payload.amount;
}
}
export default mutations;
methods: {
actionb(){
let amount = {
type: 'actionb',
amount: 5
}
this.$store.dispatch(amount).then(() => {
...
});
}
}
网友评论