vue中axios接口的封装

api.js写在src目录下api文件夹里面
api目录写个api.js

import axios from 'axios'
// 登录
export const index_info = function () {
   
	return axios.post('/api/home').then(res => {
   
		console.log(res);
		return res.data
	}).catch(err => {
   
		console.log("api登录错误", err)
	})
};

在需要调用接口的页面导入文件

import * as api from '@/api/api'

调用:

//请求数据
            getUser() {
   
                console.log();
                api.index_info()
                    .then((res) => {
   
                        console.log(res);
                    })
                    .catch((err) => {
   
                        console.log(err);
                    });
            },
全部评论

相关推荐

黑皮白袜臭脚体育生:春节刚过就开卷吗?哈基馆,你这家伙......
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务