Skip to content

Commit 95a0f63

Browse files
authored
axios.js to typescript (#47)
1 parent a330ba8 commit 95a0f63

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

AspNetCoreVueStarter.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
3030
</ItemGroup>
3131

32+
<ItemGroup>
33+
<None Remove="ClientApp\src\plugins\axios.ts" />
34+
</ItemGroup>
35+
3236
<ItemGroup>
3337
<Content Include="ClientApp\tsconfig.json" />
3438
</ItemGroup>
@@ -37,6 +41,10 @@
3741
<None Include="README.md" />
3842
</ItemGroup>
3943

44+
<ItemGroup>
45+
<TypeScriptCompile Include="ClientApp\src\plugins\axios.ts" />
46+
</ItemGroup>
47+
4048
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
4149
<!-- Ensure Node.js is installed -->
4250
<Exec Command="node --version" ContinueOnError="true">

ClientApp/src/plugins/axios.js renamed to ClientApp/src/plugins/axios.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
import Vue from 'vue';
4-
import axios from "axios";
3+
import vue from 'vue';
4+
import axios, { AxiosInstance } from "axios";
55

66
// Full config: https://github.com/axios/axios#request-config
77
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
@@ -39,23 +39,17 @@ _axios.interceptors.response.use(
3939
}
4040
);
4141

42-
Plugin.install = function(Vue, options) {
43-
Vue.axios = _axios;
44-
window.axios = _axios;
45-
Object.defineProperties(Vue.prototype, {
46-
axios: {
47-
get() {
48-
return _axios;
49-
}
50-
},
51-
$axios: {
52-
get() {
53-
return _axios;
54-
}
55-
},
56-
});
57-
};
42+
function AxiosPlugin(Vue: typeof vue, options?: any): void {
43+
Vue.prototype.$axios = _axios;
44+
}
45+
46+
declare module 'vue/types/vue' {
47+
interface Vue {
48+
$axios: AxiosInstance;
49+
}
50+
}
51+
52+
vue.use(AxiosPlugin)
5853

59-
Vue.use(Plugin)
6054

6155
export default Plugin;

ClientApp/src/views/FetchData-decorator.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
// an example of a Vue Typescript component using vue-property-decorator
4949
import { Component, Vue } from 'vue-property-decorator';
5050
import { Forecast } from '../models/Forecast';
51-
import axios from 'axios';
5251
5352
@Component({})
5453
export default class FetchDataView extends Vue {
@@ -78,7 +77,7 @@ export default class FetchDataView extends Vue {
7877
7978
private async fetchWeatherForecasts() {
8079
try {
81-
const response = await axios.get<Forecast[]>('api/WeatherForecast');
80+
const response = await this.$axios.get<Forecast[]>('api/WeatherForecast');
8281
this.forecasts = response.data;
8382
} catch (e) {
8483
this.showError = true;

ClientApp/src/views/FetchData.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
// an example of a Vue Typescript component using Vue.extend
4747
import Vue from 'vue';
4848
import { Forecast } from '../models/Forecast';
49-
import axios from 'axios';
5049
5150
export default Vue.extend({
5251
data() {
@@ -75,7 +74,7 @@ export default Vue.extend({
7574
},
7675
async fetchWeatherForecasts() {
7776
try {
78-
const response = await axios.get<Forecast[]>('api/WeatherForecast');
77+
const response = await this.$axios.get<Forecast[]>('api/WeatherForecast');
7978
this.forecasts = response.data;
8079
} catch (e) {
8180
this.showError = true;

0 commit comments

Comments
 (0)