如何在Vue(Typescript)中使用axios?

wil*_*iam 7 javascript prototype typescript vue.js

我想在vue(Typescript)中使用axios,但是我的代码遇到了麻烦。这是我的main.ts

import axios from 'axios'
Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://192.168.1.225:8088'
Run Code Online (Sandbox Code Playgroud)

这是我的Vue代码 屏幕截图

这是我第一次使用Typescript?在我以其他方式在javaScript中使用它之前,我没有任何问题,那么如何在TypeScript中使用它呢?

感谢您的时间和解决方案。

ary*_*y-T 6

我这样做并在 main.ts 上完美地工作

import Vue from 'vue';
import axios, { AxiosStatic } from 'axios';

axios.defaults.baseURL = 'http://192.168.1.225:8088';
Vue.prototype.$axios = axios;
declare module 'vue/types/vue' {
  interface Vue {
    $axios: AxiosStatic;
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 2

您确定使用 POST 请求吗?由于“GetTreeTenant”,这似乎是 GET 请求,您只能尝试 axios 而不是 $axios。

let uri = '<url here>';
  this.axios.post(uri, data).then((response) => {
    console.log(response);
});
Run Code Online (Sandbox Code Playgroud)