diff --git a/AspNetCoreVueStarter.csproj b/AspNetCoreVueStarter.csproj index 0cb2f83..bffb38f 100644 --- a/AspNetCoreVueStarter.csproj +++ b/AspNetCoreVueStarter.csproj @@ -29,6 +29,10 @@ + + + + @@ -37,6 +41,10 @@ + + + + diff --git a/ClientApp/src/plugins/axios.js b/ClientApp/src/plugins/axios.ts similarity index 75% rename from ClientApp/src/plugins/axios.js rename to ClientApp/src/plugins/axios.ts index dacf195..af4708a 100644 --- a/ClientApp/src/plugins/axios.js +++ b/ClientApp/src/plugins/axios.ts @@ -1,7 +1,7 @@ "use strict"; -import Vue from 'vue'; -import axios from "axios"; +import vue from 'vue'; +import axios, { AxiosInstance } from "axios"; // Full config: https://github.com/axios/axios#request-config // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || ''; @@ -39,23 +39,17 @@ _axios.interceptors.response.use( } ); -Plugin.install = function(Vue, options) { - Vue.axios = _axios; - window.axios = _axios; - Object.defineProperties(Vue.prototype, { - axios: { - get() { - return _axios; - } - }, - $axios: { - get() { - return _axios; - } - }, - }); -}; +function AxiosPlugin(Vue: typeof vue, options?: any): void { + Vue.prototype.$axios = _axios; +} + +declare module 'vue/types/vue' { + interface Vue { + $axios: AxiosInstance; + } +} + +vue.use(AxiosPlugin) -Vue.use(Plugin) export default Plugin; diff --git a/ClientApp/src/views/FetchData-decorator.vue b/ClientApp/src/views/FetchData-decorator.vue index c2a7d6f..eed3753 100644 --- a/ClientApp/src/views/FetchData-decorator.vue +++ b/ClientApp/src/views/FetchData-decorator.vue @@ -48,7 +48,6 @@ // an example of a Vue Typescript component using vue-property-decorator import { Component, Vue } from 'vue-property-decorator'; import { Forecast } from '../models/Forecast'; -import axios from 'axios'; @Component({}) export default class FetchDataView extends Vue { @@ -78,7 +77,7 @@ export default class FetchDataView extends Vue { private async fetchWeatherForecasts() { try { - const response = await axios.get('api/WeatherForecast'); + const response = await this.$axios.get('api/WeatherForecast'); this.forecasts = response.data; } catch (e) { this.showError = true; diff --git a/ClientApp/src/views/FetchData.vue b/ClientApp/src/views/FetchData.vue index 3f6ce45..24a4a33 100644 --- a/ClientApp/src/views/FetchData.vue +++ b/ClientApp/src/views/FetchData.vue @@ -46,7 +46,6 @@ // an example of a Vue Typescript component using Vue.extend import Vue from 'vue'; import { Forecast } from '../models/Forecast'; -import axios from 'axios'; export default Vue.extend({ data() { @@ -75,7 +74,7 @@ export default Vue.extend({ }, async fetchWeatherForecasts() { try { - const response = await axios.get('api/WeatherForecast'); + const response = await this.$axios.get('api/WeatherForecast'); this.forecasts = response.data; } catch (e) { this.showError = true;