vuex记录状态

// actions

import { queryProductDetailsById } from '../service'


/*
 * 异步
 */
export const addAndGetProduct = ({
  commit,
  state,
  getters
}, params) => {
  const index = getters.products.findIndex(item => item.productId === params.productId)
  if (index > -1) {
    return Promise.resolve(getters.products[index])
  } else {
    return queryProductDetailsById(params).then(res => {
      if (res.result === '000000') {
        const product = { ...res.data, productId: params.productId }
        commit('addProduct', product) // 提交mutations
        return product // 返回当前产品相关信息
      } else {
        Toast.fail(res.msg)
        return
      }
    }).catch(console.log)
  }
}

  

posted @ 2020-12-09 14:42  糖锡  阅读(90)  评论(0)    收藏  举报