zoukankan      html  css  js  c++  java
  • 如何将axios封装成一个插件

    01==>重新写axios的插件

    在src下创建一个插件文件为plugins  在创建一个http.js文件
    根据官方插件  重新写axios的插件  http.js文件如下
    
      import axios from "axios"
        const MyHttpServer={}
        MyHttpServer.install=(Vue)=>{
            axios.defaults.baseURL = "http://api.xiaomadagege.cn:8888/api/private/v1/"; //配置一个基础路径哈
            Vue.prototype.$http=axios
        }
        
        //暴露出去哈
        export default MyHttpServer
    

    02 在main.js中去引入

    在vue中@表示是src这个文件夹哦

    import MyServerHttp from "@/plugins/http.js";
    Vue.use(MyServerHttp);
    

    按照上面这两步插件已经发呢改装好了哈。然后就是可以使用了。
    下面是使用的方法

      //this.formLabelAlign是携带的参数
      methods: {
        handleLogin() {
          this.$http.post("login", this.formLabelAlign).then(res => {
            const {  //解构
              data,
              meta: { msg, status }
            } = res.data;
    
            if (status === 200) {
              this.$router.push({ name: "home" });
            }
          });
        }
      }
    
  • 相关阅读:
    11111
    单例-Singleton-03
    单例-Singleton-02
    单例-Singleton-01
    load和initialize
    OC中的static-01
    GCD-06
    GCD-05
    GCD-03
    UIView-01
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/12444681.html
Copyright © 2011-2022 走看看