I am very new to using Typescript, but I am not having a great time with it and Vuex/Axios.
Setup: Vue CLI app, Vue 2, Vuex 3, Axios, Typescript
High level: I have a custom Axios instance where I am setting the baseURL, interceptors, timeout settings, etc. I'm importing Axios and then using axios.create() to set my configs.
I want to use this instance of Axios from any component, or the Vuex store like so this.$api.get()
Right now, I have a file for my Axios instance:
import Vue from 'vue';
import axios from 'axios';
const $api = axios.create(
{
baseURL: 'https://myapi.com',
headers: {
...
},
timeout: 20000,
timeoutErrorMessage: 'timeout',
...
},
);
Vue.prototype.$api = $api;
In my Vuex store actions store/index.ts
export default new Vuex.Store({
...
actions: {
async fetchUser({ commit }): Promise<void> {
const path = '/me';
const user = await this.$api.get(path);
commit('setUser', user);
},
}
});
Then my main.ts file:
import Vue from 'vue';
import store from './store';
import IndexPage from './pages/index.vue';
require('./plugins/api');
Vue.config.productionTip = false;
new Vue({
store,
render: (h) => h(IndexPage),
}).$mount('#app');
But I'll get errors like this:
Property '$api' does not exist on type 'Store<{ user: User; courses: Course[]; }>'.
I've tried to use the Vue-axios npm package and access the API with Vue.$api.get()
Property '$api' does not exist on type 'VueConstructor