【发布时间】:2019-02-07 09:51:16
【问题描述】:
我正在如下配置 Nuxt.js 的 VUEX。
store
├── README.md
├── posts
│ ├── actions.js
│ ├── getters.js
│ ├── index.js
│ ├── mutations.js
│ └── utils
│ └── getPostById.js
└── index.js
我在 nuxt.config.js 中的模块中添加了@nuxtjs/axios,并且可以在操作中使用this.$axios.$get。
但是,您不能在 store/posts/utils/getPostById.js 中使用 A。
将出现Cannot read property '$axios' of undefined 的错误。
每个代码描述如下。
・ store/index.js
import Vuex from 'vuex'
import postsModule from './posts'
new Vuex.Store({
modules: {
posts: postsModule
}
})
・store/posts/index.js
import actions from './actions'
import getters from './getters'
import mutations from './mutations'
export const state = () => ({
posts: [],
post: {}
})
export default {
namespaced: true,
state,
actions,
getters,
mutations
}
・ store/posts/actions.js
import getPostById from './utils/getPostById'
export default {
async fetchPost({ commit }, count = 10) {
const params = { count: count }
// Here `$axios` can be used normally
const result = await this.$axios.$get("ApiUrl")
commit('setPosts', result)
},
async fetchPostById({ commit }, category_ids, count = 10) {
const topCommunities = {}
const result = await getPostById(id)
commit('setPost', result)
}
}
・ store/posts/utils/getPostById.js
export default async function(post_id) {
// Here `$axios` can not use
const result = await this.$axios.$get(`${ApiUrl}/${post_id}`)
return result
}
如何在getPostById.js 中使用this.$axios.$get?
【问题讨论】:
标签: javascript vue.js axios vuex nuxt.js