国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解vuex中mutation/action的傳參方式

2019-11-19 13:19:35
字體:
來源:轉載
供稿:網友

前言

在vuex中提交 mutation 是更改狀態的唯一方法,并且這個過程是同步的,異步邏輯都應該封裝到 action 里面。對于mutation/action,有一個常見的操作就是傳參,也就是官網上說的“提交載荷”。

這里是關于如何在vue-cli中使用vuex的方法,我們采用將vuex設置分割成不同模塊的方法。其中,state模塊中配置如下

//vuex中的stateconst state = {  count: 0}export default state;

mutation傳參

樸實無華的方式

mutation.js

//vuex中的mutationconst mutations = {  increment: (state,n) => {    //n是從組件中傳來的參數    state.count += n;  }}export default mutations;

vue組件中(省去其他代碼)

methods: {  add(){    //傳參    this.$store.commit('increment',5);  }}

對象風格提交參數

mutation.js

//vuex中的mutationconst mutations = {  decrementa: (state,payload) => {    state.count -= payload.amount;  }}export default mutations;

vue組件

methods: {  reducea(){    //對象風格傳參    this.$store.commit({      type: 'decrementa',      amount: 5    });  },}

action傳參

樸實無華

action.js

/vuex中的actionconst actions = {  increment(context,args){    context.commit('increment',args);  }}export default actions;

mutation.js

//vuex中的mutationconst mutations = {  increment: (state,n) => {    state.count += n;  }}export default mutations;

vue組件

methods: {  adda(){    //觸發action    this.$store.dispatch('increment',5);  }}

對象風格

action.js

//vuex中的actionconst actions = {  decrementa(context,payload){    context.commit('decrementa',payload);  }}export default actions;

mutation.js

//vuex中的mutationconst mutations = {  decrementa: (state,payload) => {    state.count -= payload.amount;  }}export default mutations;

vue組件

methods: {  reduceb(){    this.$store.dispatch({      type: 'decrementa',      amount: 5    });  }}

action的異步操作

突然就想總結一下action的異步操作。。。。

返回promise

action.js

//vuex中的actionconst actions = {  asyncMul(context,payload){    //返回promise給觸發的store.dispatch    return new Promise((resolve,reject) => {      setTimeout(() => {        context.commit('multiplication',payload);        resolve("async over");      },2000);    });  }}export default actions;

mutation.js

//vuex中的mutationconst mutations = {  multiplication(state,payload){    state.count *= payload.amount;  }}export default mutations;

vue組件

methods: {  asyncMul(){    let amount = {      type: 'asyncMul',      amount: 5    }    this.$store.dispatch(amount).then((result) => {      console.log(result);    });  }}

在另外一個 action 中組合action

action.js

//vuex中的actionconst 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;

mutation.js

//vuex中的mutationconst mutations = {  multiplication(state,payload){    state.count *= payload.amount;  }}export default mutations;

vue組件

methods: {  actiona(){    let amount = {      type: 'actiona',      amount: 5    }    this.$store.dispatch(amount).then((result) => {      console.log(result);    });  }}

使用async函數

action.js

//vuex中的actionconst 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;

mutation.js

//vuex中的mutationconst mutations = {  multiplication(state,payload){    state.count *= payload.amount;  }}export default mutations;

vue組件

methods: {  actionb(){    let amount = {      type: 'actionb',      amount: 5    }    this.$store.dispatch(amount).then(() => {      ...    });  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰州市| 四子王旗| 宁武县| 体育| 宁化县| 安塞县| 漠河县| 孝义市| 新乡市| 武汉市| 北流市| 荔浦县| 仁寿县| 武鸣县| 濮阳县| 平遥县| 冕宁县| 同江市| 鄢陵县| 资中县| 奈曼旗| 德令哈市| 通许县| 兴宁市| 玛沁县| 阳原县| 栾城县| 建宁县| 和硕县| 蛟河市| 肇州县| 平顺县| 连山| 澎湖县| 隆昌县| 临潭县| 吉林市| 惠东县| 广饶县| 恩平市| 洪江市|