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);
                    });
            },
全部评论

相关推荐

10-29 18:20
济南大学 Java
无语的太平湖水怪在看...:刷KPI的吧
点赞 评论 收藏
分享
10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务