欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PC端vue框架axios封装

程序员文章站 2022-06-21 20:07:09
一、新建api文件夹,新建axiosFun.jsimport axios from 'axios';const baseUrl = '请求域名';// 通用公用axios请求方法const req = (method, url, params) => { return axios({ method: method, url: baseUrl + url, headers: { 'Content-Type': '...

一、新建api文件夹,新建axiosFun.js

import axios from 'axios';
const baseUrl = '请求域名';
// 通用公用axios请求方法
const request = (method, url, params) => {
    return axios({
        method: method,
        url: baseUrl + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};

export {
    request
}

二、新建userMan.js,这是关于用户的请求,其它自建

import { request } from './axiosFun';

export const getIndex = (params) => { return request ("post", "接口后缀", params) };

三、页面调用

<script>
    import { getIndex } from '../../api/userMan'
    export default {
        data() {
            return {
            }
        },
        created() { 
		    this.getIndex()
        }, 
        async getIndex(){
			let res = await getIndex()
			console.log(res)
		},
    }
</script>

 

本文地址:https://blog.csdn.net/qq_40476712/article/details/108575565

相关标签: PC端