1.axios安装

  1. npm install --save axios
  2. //用于将post请求中的data对象转化为字符串
  3. npm install --save querystring

2.main.js引入axios并指定全局的$axios

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import './registerServiceWorker'
  4. import axios from "axios"
  5. const app = createApp(App)
  6. app.config.globalProperties.$axios = axios
  7. app.mount('#app')

3.在任意的组件中用$axios直接进行网络请求:

如在MyCustomComponent.vue中进行网络请求:

  1. <template>
  2. <div>
  3. <p>{{ chengpin.title }}</p>
  4. </div>
  5. </template>
  6. <script>
  7. import querystring from "querystring"
  8. export default {
  9. name: "MyCustomComponent",
  10. data() {
  11. return {
  12. chengpin: {}
  13. }
  14. },
  15. mounted() {
  16. // 用全局的$axios进行快捷get请求
  17. this.$axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res => {
  18. console.log("--------快捷get请求--------");
  19. this.chengpin = res.data.chengpinDetails[0];
  20. console.log(res.data);
  21. })
  22. // 用全局的$axios进行快捷post请求
  23. this.$axios.post("http://iwenwiki.com/api/blueberrypai/login.php", querystring.stringify({
  24. user_id: "iwen@qq.com",
  25. password: "iwen123",
  26. verification_code: "crfvw"
  27. })
  28. ).then(res => {
  29. console.log("--------快捷post请求--------");
  30. console.log(res.data);
  31. })
  32. }
  33. }
  34. </script>
  35. <style scoped>
  36. </style>

image.png

4、在src下创建utils文件夹,并在utils下创建request.js文件来封装axios

request.js文件代码如下:

  1. import axios from "axios"
  2. import querystring from "querystring"
  3. // 网络请求公共配置
  4. const instance = axios.create({
  5. timeout: 5000
  6. })
  7. // 拦截器,发送数据之前的处理
  8. instance.interceptors.request.use(
  9. config => {
  10. if (config.methods === "post") {
  11. config.data = querystring.stringify(config.data);
  12. }
  13. return config;
  14. }, error => {
  15. return Promise.reject(error);
  16. }
  17. )
  18. // 拦截器,获取数据之前的处理
  19. instance.interceptors.response.use(
  20. response => {
  21. return response.status === 200 ? Promise.resolve(response) : Promise.reject(response);
  22. }, error => {
  23. const { response } = error;
  24. errorHandle(response.status, response.info);
  25. }
  26. )
  27. // 错误码
  28. const errorHandle = (status, info) => {
  29. switch (status) {
  30. case 400:
  31. console.log("语义有误");
  32. break;
  33. case 401:
  34. console.log("服务器认证失败");
  35. break;
  36. case 403:
  37. console.log("服务器拒绝访问");
  38. break;
  39. case 404:
  40. console.log("地址错误");
  41. break;
  42. case 500:
  43. console.log("服务器遇到意外");
  44. break;
  45. case 404:
  46. console.log("服务器无响应");
  47. break;
  48. default:
  49. console.log(info);
  50. break;
  51. }
  52. }
  53. export default instance;

5、在src下添加api文件夹,并在api文件夹下添加path.js文件

path.js文件代码如下:

  1. const base = {
  2. baseUrl:"http://iwenwiki.com",
  3. chengpin:"/api/blueberrypai/getChengpinDetails.php"
  4. }
  5. export default base;

6、在api文件夹下添加index.js文件

index.js文件代码如下:

  1. import axios from '../utils/request'
  2. import path from './path'
  3. const api = {
  4. // 获取成品详情
  5. getChengpin(){
  6. return axios.get(path.baseUrl + path.chengpin);
  7. }
  8. }
  9. export default api;

7、此时便可在任意的组件中引入api进行网络调用了。

比如在MyCustomComponent.vue文件中使用:

  1. <template>
  2. <div>
  3. <p>{{ chengpin.title }}</p>
  4. </div>
  5. </template>
  6. <script>
  7. import querystring from "querystring"
  8. import api from "../api/index"
  9. export default {
  10. name: "MyCustomComponent",
  11. data() {
  12. return {
  13. chengpin: {}
  14. }
  15. },
  16. mounted() {
  17. // 用全局的$axios进行快捷get请求
  18. this.$axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res => {
  19. console.log("--------快捷get请求--------");
  20. this.chengpin = res.data.chengpinDetails[0];
  21. console.log(res.data);
  22. })
  23. // 用全局的$axios进行快捷post请求
  24. this.$axios.post("http://iwenwiki.com/api/blueberrypai/login.php", querystring.stringify({
  25. user_id: "iwen@qq.com",
  26. password: "iwen123",
  27. verification_code: "crfvw"
  28. })
  29. ).then(res => {
  30. console.log("--------快捷post请求--------");
  31. console.log(res.data);
  32. })
  33. // 用封装的axios进行快捷网络请求
  34. api.getChengpin().then(res => {
  35. console.log("--------用封装的axios进行快捷网络请求--------");
  36. console.log(res.data);
  37. })
  38. }
  39. }
  40. </script>
  41. <style scoped>
  42. </style>

效果图:
image.png

8、如果不想在组件中重复引入api的操作,也可以把api的引入封装到main.js中去

main.js:

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import './registerServiceWorker'
  4. //import axios from "axios"
  5. import api from './api/index.js'
  6. const app = createApp(App)
  7. //app.config.globalProperties.$axios = axios
  8. app.config.globalProperties.$api = api
  9. app.mount('#app')

如果无法引入api,配置一下tsconfig.json文件,添加 “noImplicitAny”: false,即可
image.png
MyCustomComponent.vue组件中:

  1. <template>
  2. <div>
  3. <p>{{ chengpin.title }}</p>
  4. </div>
  5. </template>
  6. <script>
  7. //import querystring from "querystring"
  8. export default {
  9. name: "MyCustomComponent",
  10. data() {
  11. return {
  12. chengpin: {}
  13. }
  14. },
  15. mounted() {
  16. /*
  17. // 用全局的$axios进行快捷get请求
  18. this.$axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res => {
  19. console.log("--------快捷get请求--------");
  20. this.chengpin = res.data.chengpinDetails[0];
  21. console.log(res.data);
  22. })
  23. // 用全局的$axios进行快捷post请求
  24. this.$axios.post("http://iwenwiki.com/api/blueberrypai/login.php", querystring.stringify({
  25. user_id: "iwen@qq.com",
  26. password: "iwen123",
  27. verification_code: "crfvw"
  28. })
  29. ).then(res => {
  30. console.log("--------快捷post请求--------");
  31. console.log(res.data);
  32. })
  33. */
  34. // 用封装的axios进行快捷网络请求
  35. this.$api.getChengpin().then(res => {
  36. console.log("--------用封装的axios进行快捷网络请求--------");
  37. console.log(res.data);
  38. })
  39. }
  40. }
  41. </script>
  42. <style scoped>
  43. </style>

效果:
image.png