首页 > 其他分享 >axios的挂载

axios的挂载

时间:2023-06-19 09:33:05浏览次数:35  
标签:axios vue res Vue api 挂载

axios的挂载

请求示例

如下,每一次都要导入axios,每一个组件都相当于vue的实例。

 

<template>
 <div class="left-container">
   <h3>left</h3>
   <button @click="getInfo">GET</button>
 </div>
</template>

<script>
import axios from 'axios'
export default {
 name: 'leftContainer',
 methods: {
   async getInfo () {
     const { data: res } = await axios.get('https://ajax-base-api-t.itheima.net/api/get')
     console.info(res)
  }
}
}
</script>
<template>
 <div class="right-container">
   <h3>right</h3>
   <button @click="postInfo">POST</button>
 </div>
</template>
<script>
import axios from 'axios'
export default {
 name: 'rightContainer',
 methods: {
   async postInfo () {
     const { data: res } = await axios.post('https://ajax-base-api-t.itheima.net/api/post', { name: 'zs', age: 20 })
     console.info(res)
  }
}
}
</script>

挂载

直接将axios挂载到Vue原型上并配置请求根路径。

/* eslint-disable */
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios' //项目只用导入一次

Vue.config.productionTip = false

//全局配置axios的请求根路径。
axios.defaults.baseURL = ' https://ajax-base-api-t.itheima.net' //请求根路径。

//在vue原型上挂载一个共享成员:把axios挂载到Vue.prototype上,供每个.vue组件的实例直接使用。
//Vue.prototype.axios = axios两种写法都可以,仅是名字不同。
Vue.prototype.$http = axios //$前缀标识这是Vue实例的一个特殊属性。

//组件中 const { data: res } = await this.$http.get('/api/get')

//今后在每个.vue组件中发起请求,直接调用this.$http.xxx
//但是,把axios挂载到vue原型上,缺点是不利于api的调用

new Vue({
 render: h => h(App)
}).$mount('#app')
//组件的使用
methods: {
async getInfo () {
const { data: res } = await this.$http.get('/api/get')
   console.info(res)
}
}

 

缺点

无法实现api接口的复用。

标签:axios,vue,res,Vue,api,挂载
From: https://www.cnblogs.com/yxxcl51/p/17490296.html

相关文章

  • 多个物理磁盘挂载到同一目录的方法 (lvm 软raid)
    多个物理磁盘挂载到同一目录的方法(lvm软raid)背景公司里面的一台申威3231的机器因为这个机器的raid卡没有操作界面.所以只能够通过命令行方式创建raid自己这一块比较菜,想着先尝试使用lvm的方式进行软raid挂载,也验证一下性能.所以写一些这个文章简单的结论虽然lv......
  • 【React工作记录一百一十二】React(Hook)+TS+axios+ant design+json server实现todoli
    前言大家好我是歌谣最近开始在做关于前端扫盲的一些只是处理花了一周左右录制了了一个hook写法的关于todoList的视频主要用于基础知识的一个使用和处理目录#前端巅峰人才交流群私信我#第一节创建项目todolist项目技术选型React(Hook)+TS+axios+antdesign+jsonserve......
  • linux挂载新磁盘
    linux挂载新磁盘1.老系统查看1.1查看磁盘挂载情况#df-hFilesystemSizeUsedAvailUse%Mountedonudev3.8G03.8G0%/devtmpfs770M1.3M769M1%/run/dev/vda159G48G9.0G85%/tmpfs3.8G03.8G0%/dev/shmtmpfs5.0M05.0M0%/run/locktmpfs3.8G0......
  • vue封装axios的方式(一)
    vue封装axios的方式(一)直接将下面代码复制在request.js中,封装了get,post请求,需要自己配置的是:自己的请求地址,tokenKey是否为token,改为自己存入本地的token名,/**axios封装*请求拦截、相应拦截、错误统一处理*/importaxiosfrom'axios';importQSfrom'qs';importrout......
  • vue封装axios的方式(二)
    vue封装axios的方式(二)创建utils\request.jsimportaxiosfrom'axios'import{Message,MessageBox}from'element-ui'//导入element-ui组件库//创建axios的对象constinstance=axios.create({baseURL:"https://api.cat-shop.penkuoer.com&quo......
  • vue封装axios的方式(三)
    vue封装axios的方式(三)与第二种封装基本相同,不同之处在于api的处理格式//引入axiosimportaxiosfrom'axios'//引入qs模块,对数据进行序列化importQSfrom'qs'//引入message模块,toast提示import{message}from'ant-design-vue'//引入storage模块,操作tokenimport......
  • axios最最基础的用法
    axiosajax请求库,比较热门吗,这里是最基础的发送请求和接收参数  axios闲来无事一,axios的理解和使用一,axios的理解和使用服了,看弹幕给我看的没自信了。axios最基本的使用方式<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv......
  • axios headers设置发送接收文件类型
    接受blobexportfunctionexportTeacherActivitiesState(data:any):Promise<IResponse<Blob>>{ returnrequest({  baseURL,  url:"/Api/CourseEvaluationManage/ActivityStatistics/ExportTeacherActivitiesState",  method:"post......
  • 记一次ubuntu系统磁盘无法挂载之gdisk命令的使用
    可以使用fdisk-l查看到磁盘分区信息但实际上并未成功(base)root@ywb:~#fdisk-l......TheprimaryGPTtableiscorrupt,butthebackupappearsOK,sothatwillbeused.Disk/dev/sde:4.6TiB,5000981077504bytes,9767541167sectorsUnits:sectorsof1*51......
  • CentOS搭建NFS服务、windows挂载权限问题; NFS读写缓慢调优
    CentOS搭建NFS服务、windows挂载权限问题CentOS和统信UOS(与ubuntu一样)命令不一样,但操作大同小异,参考如下:https://blog.csdn.net/wangzongyu/article/details/127009158 windows读写linux搭建的NFS内文件是比较慢,调优:https://blog.csdn.net/yysalad/article/details/11736623......