Vue 中使用 TypeScript axios 使用方式

Vue 中使用 TypeScript

axios 使用方式

方式一

import axios from 'axios';
Vue.prototype.$axios = axios;

// 在 .vue 文件中使用
// 使用 this as any 是去掉 ts 的类型检测 axios 是挂载成功的
(this as any).$axios.get('http://www.baidu.com').then((res: any) => {
      console.log(res);
    });

方式二

import axios, { AxiosInstance } from 'axios'

declare module 'Vue/types/vue' {
  interface Vue {
    $http: AxiosInstance
  }
}

// 在 .vue 文件中使用
this.$axios.get('http://www.baidu.com').then((res: any) => {
      console.log(res);
    });

方式三

import axios from 'axios';
import VueAxios from 'vue-axios';

Vue.use(VueAxios, axios)

// 在 .vue 文件中使用
this.$axios.get('http://www.baidu.com').then((res: any) => {
    console.log(res);
});

方式四

// 在 .vue 文件中直接导入
import axios from "axios";

axios.get('http://www.baidu.com', {}).then((res: any) => {
    console.log(res);
});

后续方式持续补充

posted @ 2021-10-29 11:05  影的记忆  阅读(1233)  评论(0编辑  收藏  举报