Vuejs2 通过vuex store中的axios获取单个记录

Vuejs2 通过vuex store中的axios获取单个记录,vuejs2,vuex,nuxt.js,vuex-modules,Vuejs2,Vuex,Nuxt.js,Vuex Modules,我想使用Nuxt中的Vuex存储模块从后端返回一条记录 我的组件中有以下内容,它传递我想要的值 (这是$route.params.caseId) 我将$route.params.caseId传递到vuex存储模块中的getCase操作中,如下所示 getCase ({ commit, context }, data) { return axios.get('http' + data + '.json') .then(res => { const convertcase =

我想使用Nuxt中的Vuex存储模块从后端返回一条记录

我的组件中有以下内容,它传递我想要的值 (这是$route.params.caseId)

我将$route.params.caseId传递到vuex存储模块中的getCase操作中,如下所示

getCase ({ commit, context }, data) {
  return axios.get('http' + data + '.json')
  .then(res => {
    const convertcase = []
    for (const key in res.data) {
      convertcase.push({ ...res.data[key], id: key })
    }
    //console.log(res.data) returns my record from firebase (doesnt display the key though in the array, just the fields within the firebase record, but assume this is expected?

    commit('GET_CASE', convertcase)
  })
  .catch(e => context.error(e));
},
convert案例是从firebase密钥中提取id,并将其作为id字段添加到我的数组中(以这种方式从firebase获得单个结果是否正确?)

我的变异

// Get Investigation
GET_CASE(state, caseId) {
  state.caseId = caseId;
},
现在当我使用案例名称:{{caseId.Case_Name}}

我没有得到任何结果

虽然我没有犯错,但请你想想我做错了什么


您可以像在dispatch方法中那样将更多数据传递给操作,然后再正常访问它们

注意getCase函数的数据参数,在您的示例中,data==$route.params.caseId

//荷载单一调查

getCase ({ commit, context }, data) {
  return axios.get('http' + investigationID + '.json')
  .then(res => {
    const convertcase = []
    for (const key in res.data) {
      convertcase.push({ ...res.data[key], id: key })
    }
    commit('GET_CASE', convertcase)
  })
  .catch(e => context.error(e));
},
如果你想使用承诺,请查看下面我的应用程序中获取单个博客帖子的操作示例

let FETCH_BLOG_POST = ({commit, state}, { slug }) => {

    return new Promise((resolve, reject) => {

        fetchBlogPost(slug, state.axios)
        .then((post) => {
            console.log("FETCH_BLOG_POSTS", post.data.data)
            commit('SET_BLOG_POST', post.data.data)
            resolve(post.data)
        })
        .catch((error) => {
            console.log("FETCH_BLOG_POST.catch")
            reject(error)
        })

    });
}

谢谢你的回复,一半解决了我的问题,但没有得到任何结果,首先尝试第一种方法,看看我是否能让事情正常进行,请查看编辑原始问题,知道我的问题是什么吗
let FETCH_BLOG_POST = ({commit, state}, { slug }) => {

    return new Promise((resolve, reject) => {

        fetchBlogPost(slug, state.axios)
        .then((post) => {
            console.log("FETCH_BLOG_POSTS", post.data.data)
            commit('SET_BLOG_POST', post.data.data)
            resolve(post.data)
        })
        .catch((error) => {
            console.log("FETCH_BLOG_POST.catch")
            reject(error)
        })

    });
}