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