Advertisement

在Vue+TS中使用全局绑定的方式实现axios、storage和接口地址的绑定

阅读量:

全局绑定axios(在TypeScript中)

1.借助cnpm工具将相关模块集成至项目目录中。

复制代码
    cnpm i axios --save
    
    
      
    
  1. 在main.ts文件中导入以下接口及组件
复制代码
    import Axios,{AxiosInstance} from  'axios'
    
    
      
    
  1. 在main.ts文件中作出如下定义
复制代码
    declare module '@vue/runtime-core' {
      interface ComponentCustomProperties {
    $axios: AxiosInstance
      }
    }
    
    
      
      
      
      
      
    
  1. 该途径可直接利用this.$axios实现调用操作。
复制代码
    requestData() {
      this.$axios.get('https://s.itying.com/api/v1/login').then(res => {
    console.log(res);
      })
    }
    
    

全部评论 (0)

还没有任何评论哟~