I encountered an issue while working with Nuxt.js and TypeScript. In my project, I rely on dependencies such as axios or nuxt-i18n. As an example, let's focus on Axios. I followed the configuration outlined in the official documentation for nuxt/axios.
nuxt.config.js
export default {
modules: ['@nuxtjs/axios']
axios: {},
}
tsconfig.json
{
"compilerOptions": {
"types": [
"@nuxt/types",
"@nuxtjs/axios"
]
}
}
Now, I aim to utilize $axios within my Vuex store as demonstrated below:
{
actions: {
async getIP ({ commit }) {
const ip = await this.$axios.$get('http://icanhazip.com')
commit('SET_IP', ip)
}
}
}
However, I am currently facing the following error message:
`TS2339: Property '$axios' does not exist on type '{ getIP(state: any, { commit }: { commit: any; }): Promise ; }'.`
Edit: Comment suggestions indicate that using an arrow function might resolve this issue. I attempted the following approach:
export const actions = {
getIP: async ({commit}) => {
const ip = await this.$axios.$get('http://icanhazip.com')
...
}
};
Unfortunately, a new error related to "this." arises:
TS7041: The containing arrow function captures the global value of 'this'.