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);
});

后续方式持续补充

最新文章

  1. Mac 配置 php-fpm 时出现'/private/etc/php-fpm.conf': No such file or directory (2)
  2. Convert PLY to VTK Using PCL 1.6.0 使用PCL库将PLY格式转为VTK格式
  3. form表单提交路径action="" 时的一种特殊情况
  4. MFC 关于如何实现浏览文件
  5. Webpack使用教程三(webpack-dev-server)
  6. C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作
  7. vs2012生成的项目,如何在只装有VS2010的电脑上打开
  8. nginx 采用https 协议通信配置
  9. Windows7中搭建Android x86_64及armv8-a操作步骤
  10. OCP-1Z0-051-题目解析-第33题
  11. java 中通过label跳出双重for 循环
  12. JavaScript中判断鼠标按键(event.button)
  13. 关于Java,那些我心存疑惑的事(不断更新中...)
  14. centos7.6设置sftp服务
  15. .NET Framework简介
  16. sysbench工具安装使用
  17. boost 实现http断点续传
  18. Dubbo的学习
  19. 使用sp_executesql
  20. linux php7.1 安装redis扩展

热门文章

  1. FastAPI 学习之路(五十三)根据环境不同连接不同数据库
  2. 23.合并k个有序链表
  3. UltraSoft Scrum Meeting 博客汇总
  4. [Beta]the Agiles Scrum Meeting 10
  5. Windows平台编译器相关的几个预定义宏
  6. 零基础学习Linux心得总结
  7. Linux入门需要搞清楚的思路问题
  8. 个人宽带如何开启IPv6网络访问
  9. 彻底搞通TCP滑动窗口
  10. Python 类似 SyntaxError: Non-ASCII character '\xc3' in file