zoukankan      html  css  js  c++  java
  • 实现网页中类似vue.use(element)方法,自建高大上组件

    在方法实现上,使用vue中axios方法封装实现例子

    import axios from 'axios'     //导入axios 包
    axios.defaults.baseUrl="localhost:8080";
    axios.interceptors.request.use((config)=>{
        let token=localstorage.getItem("token");  //本地取token
        if(token){
        config.headers.token="token"; //设置头部携带token;
    }
    return config;               //记得返回config哦,不然数据请求会出错
    });         //一个简单的axios封装已经实现,接下来实现要实现的vue.use()效果
    
    const install=(vue)=>{              //核心,通过install事件注册;
        if(install.installed ){
         return;               //返回不执行下一步
    }
    vue.prototype.$api=axios;
    }
    export default install;
    
    //main.js,接下来就可以在main.js中引用实现vue.use(效果了o)
    import api from 'axios封装所在目录'
    vue.use(api)          //效果实现,是不是很简单
    

      

  • 相关阅读:
    Python异常处理
    奇异值分解(SVD)详解及其应用
    上楼梯问题
    Python面向对象(特殊成员)
    Best Time to Buy and Sell Stock II
    String to Integer (atoi)
    Gas Station
    N-Queens II
    Letter Combinations of a Phone Number
    N-Queens
  • 原文地址:https://www.cnblogs.com/uimeigui/p/11788479.html
Copyright © 2011-2022 走看看