首页 > 其他分享 >Vue 3.x 中使用 Axios 请求远程Api接口数据

Vue 3.x 中使用 Axios 请求远程Api接口数据

时间:2022-11-17 16:23:05浏览次数:67  
标签:axios 请求 get 使用 Vue Axios Api import

一、安装axios插件

npm install axios --save
//或者 yarn add axios
//或者 cnpm install axios --save

 

注:安装包的时候 后面的 --save,如果不加,只安装在当前项目,把代码发给别人,是运行不了的,所以安装工具类包等,尽量都加上 --save

二、如何使用(github说明

1、在需要使用的组件中引入 axios模块import axios from 'axios'

2、使用 axios.get 或者 axios.post 请求数据

<template>
  <div>
    <button @click="getData()">获取api数据</button>
  </div>
</template>

<script>
//1、引入 axios 模块
import axios from 'axios'
export default {
  data() {
    return {
      msg: "主页"
    }
  },
  methods:{
   getData(){
      var api="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
      //2.使用axios 进行get请求
      axios.get(api).then((res)=>{
        //请求成功的回调函数
        console.log(res)
      }).catch((err)=>{
        //请求失败的回调函数
        console.log(err)
      })
   }

  }
};
</script>

 

三、全局配置 axios

1、在 main.js中全局配置

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Axios from 'axios';//引入axios

var app=createApp(App)
app.config.globalProperties.Axios=Axios //全局配置axios

app.use(store).use(router).mount("#app");

 

小心得:自己写的属性,如果有很多地方使用,也可以使用全局绑定,比如我们写了一个 storage.js 模块,如果大量地方使用,我们就可以全局注册,
方式一样,也是先在 main.js 中引入,如何使用app.config.globalProperties.Storage=Storage ,然后在需要用到的地方使用 this.Storage就可以调用了

2、使用的时候直接使用 this.Axios.get 或者 this.Axios.post 请求接口

 getData(){
      var api="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
      //2.使用axios 进行get请求
      this.Axios.get(api).then((res)=>{
        //请求成功的回调函数
        console.log(res)
      }).catch((err)=>{
        //请求失败的回调函数
        console.log(err)
      })
   }

 

标签:axios,请求,get,使用,Vue,Axios,Api,import
From: https://www.cnblogs.com/Ryan-DHQ/p/16899840.html

相关文章

  • vuedemo
    一.创建环境1.创建D:\code\vue文件夹2.vscode打开文件夹3.打开终端,输入npminstall-g@vue/cli4.配置环境变量终端输入:npmconfiglist找到路径将路径加入......
  • 升级node.js造成vue启动报错:digital envelope routines::unsupported
    原文:https://blog.csdn.net/qq_45039822/article/details/126195373今天把node.js升级到了最新版v18.12.1,启动vue项目时报错:digitalenveloperoutines::unsupported,在网......
  • Vue 中 watch 监听器与 computed 计算属性的使用
    一、watch:监视单个属性,可做简单监视和深度监视。根据数据的具体结构,决定是否采用深度监视。配置deep:true可以监测对象内部值的改变,做深度监视时使用。1、简单监视:监视单......
  • Vue mixin(混入)
    1、mixin配置mixin中文叫混入或者混合,作用是可以把多个组件共用的配置提取成一个混入对象,就是提取成共用一个配置2、定义mixin混入(mixin)的定义需是一个对象,然后Vue里的......
  • WindowsAPI示例-C#版_监控usb设备插拔
    1、Winform代码:publicpartialclassUSBDeviceMode:Form{publicUSBDeviceMode(){InitializeComponent();UsbN......
  • WindowsAPI-C#版_句柄回调常用通知类型汇总(HandleNotification)
    包含自定义的HandleNotification_MessageMsg、HandleNotification_MessageWParam类型,如下:/***┌──────────────────────────────────......
  • vue router keepalive 未生效的原因
    组件文件名称和组件名称不一致。如图,文件名称为index.vue,但是name为Review,两者不一致,导致缓存页面不成功。 解决方案:第一种:在index.vue中手动设置component的名称为......
  • 尚硅谷vue课程之vue中的mvvm
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content......
  • Vue新老系统切换(By 权)
    Vue新老系统切换1-    卸载node.js2-   https://github.com/coreybutler/nvm-windows/releases 下载nvm 3..安装后cmd  nvm-v 4.cmd里输入 nvmi......
  • vue 解决el-table 表体数据发生变化时,未重新渲染问题|页面不更新问题
    一、所遇到的问题在使用el-table组件时,数据已经发生了变化,但是页面显示的数据却没变化;二、解决办法在el-table中添加一个key,可以设置成boolean类型的,在数据更新后更新......