【问题标题】:Calling Vuex action from within another vuex action从另一个 vuex 操作中调用 Vuex 操作
【发布时间】:2022-01-23 23:20:42
【问题描述】:

我最近迁移到Vuex 商店,并且取得了不错的进展。但我目前面临的挑战是从 Vuex 的另一个动作中调用一个动作。我还在学习Vue.js的过程中

Current versions

  1. Vue 3
  2. Vuex 4
  3. 如果需要,我使用Electron Vue.js dev tools

/src/store/index.js - 当前代码

/* eslint-disable no-redeclare */

import { createStore } from 'vuex'
import axios from 'axios'

export default createStore({
  state: {
    ...
  },
  mutations: {
    setQuery(state, query) {
      // refers this.state.query
      state.query = query
    },

    setOnlinePresenceInitialPageLoad(state, presence) {
      // refers this.state.online & this.state.initialPageLoad
      state.online = presence
      state.initialPageLoad = false
    },

    setRequestErrorAndStatus(state, { _status, code }) {
      // refers this.state.request.error & this.state.request._status
      state.request.error = _status
      state.request._status = code
    },

    setResults(state, processed) {
      state.request.results = processed
    }
  },
  actions: {
    callApi({ commit, state, dispatch }) {
      axios.get(state.axios.targetURL, {
        baseURL: state.axios.config.baseURL,
        params: {
          days: state.axios.config.params.days,
          q: state.query,
          key: state.axios.config.params.key,
        },
      }).then(response => {
        console.log(response.status)
        commit('setOnlinePresenceInitialPageLoad', true);
        dispatch('formatResults', response.data);
      }).catch(error => {
        if (error.response) {
          console.log(error.response.status)
        } else if (error.request) {
          console.log(error.request.status)
        } else {
          console.log("Couldn't find the problem")
        }
      })
    },
    formatResults(context, payload) {
      const processedData = {
          ...
      }
      console.log(processedData);
      context.commit('setResults', processData);
    }
  },
  modules: {
  }
})

如您所见,callApi() 在 Promise 的已实现部分调用 formatResults()

Current state (Web browser)

在代码中,我尝试注销变量processedData。我以为它会打印在控制台上。

Current state (Vue Devtools)

我也想知道为什么formatResults() 永远不会结束。

这个问题可以用异步函数解决吗,如果可以,我想知道要采取的程序吗?

感谢您的帮助

【问题讨论】:

  • 您的屏幕截图并没有告诉我们太多信息。你能在 formatResults` 的开头和结尾放一个console.log 并确认第二个永远不会触发吗?并且等价地在callAPI? 中调度它之前和之后
  • 问题是火灾但由于某种原因永远不会结束

标签: javascript vue.js vuejs3 vuex4 vue-devtools


【解决方案1】:

从提供的信息中很难判断,但我将在这里冒险猜测......

看到console.log(response.status) 然后console.log("Couldn't find the problem") 被触发,以及formatResults(来自vuex 截图)我怀疑formatResults 正在抛出错误。

formatResults(context, payload) {
  const processedData = {
      // the error could be somewhere here
  }
  console.log(processedData);
  context.commit('setResults', processData);
}

当错误发生时,catch 会处理它

if (error.response) {
  console.log(error.response.status)
} else if (error.request) {
  console.log(error.request.status)
} else {
  // handled here with log ...
  console.log("Couldn't find the problem")
}

尝试使用console.error(error)查看错误原因是什么

if (error.response) {
  console.log(error.response.status)
} else if (error.request) {
  console.log(error.request.status)
} else {
  console.error(error)
}

然后你可能有足够的信息来调试问题的根源

【讨论】:

  • 谢谢我找到了问题
  • ? 必须注意错误处理。我建议使用自定义错误类型和instance of 来验证您正在处理正确的错误。 (有关更多信息,请参阅 stackoverflow.com/questions/1382107/…)然后对于 else 使用 throw error 以便未处理的错误冒泡
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 2019-06-11
  • 2018-11-26
  • 2019-08-16
  • 2018-10-17
  • 2021-11-28
  • 2017-05-22
相关资源
最近更新 更多