StarBlog Blog Development Notes: Frontend Development (5) - Axios Network Request Wrapper
Install Axios Axios is a Promise-based HTTP client for browsers and Node.js. We will need it later to send HTTP requests, so let's first record the installation and usage of Axios. Installation command yarn add axios Write code Modify src/views/Home.vue First, adjust the layout template div class=page h2Home Page/h2 el-alert
目录16 个章节
Installing axios
axios is a Promise-based HTTP client for browsers and Node.js. We'll need it later to send HTTP requests, so let's first document the installation and usage of axios.
Installation command
yarn add axios
Writing Code
Modify src/views/Home.vue
First, adjust the layout by adding a button to trigger the network request logic.
<template>
<div class="page">
<h2>Home Page</h2>
<el-alert
title="主页"
type="success">
</el-alert>
<el-button type="primary" @click="testAxios()">Test Axios</el-button>
</div>
</template>
Next is the script section below.
import axios from 'axios'
export default {
name: "Home",
methods: {
testAxios() {
axios.get('http://localhost:5038/Api/Category/1')
.then(res => console.log(res.data))
}
}
}
Test the Page
Open the page and click the button.

You can see that the console has already printed out the requested data, nice
Encapsulating Axios
Using axios to send a request is relatively straightforward, but without proper encapsulation and reuse, axios can lead to increasing code redundancy as the project grows. This makes the code harder to maintain. Therefore, we will first perform a secondary encapsulation of axios here, enabling request reuse across different components in the project and making the code easier to maintain.
Requirements
- Unified URL configuration.
- Unified API requests.
- Request interceptors. For example: attach tokens, set request headers, etc.
- Response interceptors. For example: unified error handling, page redirection, etc.
- Combine Vuex for global loading animations or error handling as needed.
- Encapsulate axios into a Vue plugin for use.
Directory Structure
Under the src directory, create a new folder named http to store the code for HTTP interaction APIs. The file structure is as follows:
http
├── modules // Sub-module APIs for user management, blog management, etc.
│ ├── blog.js
│ ├── category.js
│ ├── login.js
│ ├── photo.js
│ └── user.js
├── api.js // Aggregated request interface module, collects all module APIs.
├── axios.js // Secondary encapsulation of the axios module, includes interceptors and other details.
├── config.js // Default axios configuration, includes base path and other settings.
└── index.js // Encapsulates axios as a plugin, to be imported as a plugin.
Installing js-cookie
In the axios module we've encapsulated, cookies are used to obtain the token. Therefore, the related dependencies need to be installed. Execute the following command
yarn add js-cookie
Continue Writing Code
/src/utils/global.js
We place some global configurations, constants, and methods in this file.
/**
* Global constant and method encapsulation module
* Mounted to Vue properties via prototype
* Called via this.Global
*/
// Backend management system server address
export const baseUrl = 'http://localhost:5038'
export default {
baseUrl
}
config.js
Axios configuration settings. Each configuration item is documented with explanations, as detailed in the comments.
import {baseUrl} from '@/utils/global'
export default {
method: 'get',
// 基础url前缀
baseUrl: `${baseUrl}/Api`,
// 请求头信息
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
// 参数
data: {},
// 设置超时时间
timeout: 10000,
// 携带凭证
withCredentials: true,
// 返回数据类型
responseType: 'json'
}
axios.js
Axios interceptor, capable of performing request interception and response interception, executing certain operations when sending and receiving requests.
- This imports configuration file information (such as baseURL, headers, withCredentials settings, etc.) into the axios object.
- When sending a request, it obtains the token. If the token does not exist, it means the user is not logged in, and it redirects to the system login page; otherwise, it continues sending the request with the token.
- If necessary, the response interceptor can be used here to uniformly process the returned results before returning them.
The code is as follows
import axios from 'axios';
import config from './config';
import Cookies from "js-cookie";
import router from '@/router'
export default function $axios(options) {
return new Promise((resolve, reject) => {
const instance = axios.create({
baseURL: config.baseUrl,
headers: config.headers,
timeout: config.timeout,
withCredentials: config.withCredentials
})
// request interceptor
instance.interceptors.request.use(
config => {
let token = Cookies.get('token')
// Send request with token
if (token) {
config.headers.token = token
} else {
// Redirect to the login page
router.push('/login')
}
return config
},
error => {
// When a request error occurs
console.log('request:', error)
// Check for request timeout
if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
console.log('timeout: request timed out')
}
// Redirect to an error page if needed
const errorInfo = error.response
console.log(errorInfo)
if (errorInfo) {
error = errorInfo.data // The detailed error information can be obtained in the catch block on the page side, see the Promise.reject at the bottom
const errorStatus = errorInfo.status; // 404 403 500 ...
router.push({
path: `/error/${errorStatus}`
})
}
return Promise.reject(error) // The error information you want to return can be obtained (caught) on the calling side
}
)
// response interceptor
instance.interceptors.response.use(
response => {
return response.data
},
err => {
if (err && err.response) {
switch (err.response.status) {
case 400:
err.message = 'Request Error'
break
case 401:
err.message = 'Unauthorized, please log in'
break
case 403:
err.message = 'Access Denied'
break
case 404:
err.message = `Request URL Error: ${err.response.config.url}`
break
case 408:
err.message = 'Request Timeout'
break
case 500:
err.message = 'Internal Server Error'
break
case 501:
err.message = 'Service Not Implemented'
break
case 502:
err.message = 'Gateway Error'
break
case 503:
err.message = 'Service Unavailable'
break
case 504:
err.message = 'Gateway Timeout'
break
case 505:
err.message = 'HTTP Version Not Supported'
break
default:
}
}
console.error(err)
return Promise.reject(err) // Return the error information returned by the API
}
)
// Request handling
instance(options).then(res => {
resolve(res)
return false
}).catch(error => {
reject(error)
})
})
}
index.js
Here, axios is registered as a Vue plugin, and the api module is mounted on Vue's prototype as the $api object. This way, wherever a this reference is available, you can call APIs using this.$api.submodule.method.
The code is as follows:
// Import all interfaces
import api from './api'
const install = Vue => {
if (install.installed)
return;
install.installed = true;
Object.defineProperties(Vue.prototype, {
// Note: Mounted on Vue's prototype as the $api object
$api: {
get() {
return api
}
}
})
}
export default install
api.js
This module is an aggregation module (聚合模块) that collects all sub-module APIs under the modules directory.
// Unified interface integration module
import * as blog from './modules/blog'
import * as category from './modules/category'
import * as login from './modules/login'
import * as photo from './modules/photo'
import * as user from './modules/user'
// Default export all
export default {blog, category, login, photo, user}
modules/blog.js
There are too many submodules under the modules directory, making it inconvenient to list them all. Here, the blog management module is used as an example.
import axios from '../axios'
// 获取博客列表
export const getList = (categoryId, page, pageSize) => {
return axios({
url: 'BlogPost',
method: 'get',
data: {categoryId, page, pageSize}
})
}
// 获取指定博客文章
export const get = postId => {
return axios({
url: `BlogPost/${postId}/`,
method: 'get',
})
}
/src/main.js
Modify main.js to import the API module and register it using Vue.use(api). This allows you to call backend interfaces via this.$api.submodule.method.
Import the global module and mount it with Vue.prototype.global = global. This enables access to global configurations through this.global.xx.
import Vue from 'vue'
import App from './App'
import router from './router'
import api from './http'
import global from '@/utils/global';
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
// Import Element
Vue.use(ElementUI)
// Import API module
Vue.use(api)
// Mount global configuration module
Vue.prototype.global = global
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: {App},
template: '<App/>'
})
Completed
OK, the Axios encapsulation is done here. I won't test it here~~(lazy)~~
With the preparation work basically complete, it's time to start writing the page.
_EOF_
StarBlog Blog Development Notes: Frontend Development (5) - Axios Network Request Wrapper
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
StarBlog博客开发笔记:前端开发(1)准备篇
环境准备 NodeJs 首先需要安装NodeJs 下载地址:http://nodejs.cn/download/ 安装前端工具链 国内使用NPM需要设置国内镜像才能正常安装,之前常用的淘宝镜像说是要停止解析了,可以用这个npmmirror 中国镜像,命令如下: npm config set registry https://registry.npmmirror.com npmmirror 中国镜像
2024-12-10 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端开发(6)之登录页面
前言 前端开发的准备工作在前面已经完成了,接下来就要开启写页面(Copy/Paste)操作了~ 用ElementUI写页面很简单,对照着官网文档代码一顿复制粘贴就完事了。 写代码 没啥好说的,直接代码 布局 template el-form :model=loginForm :rules=fieldRules ref=loginForm label-position=left label-wi
2023-07-14 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端开发之导航守卫
路由对象router给我们提供了beforeEach方法,可以在每次路由之前进行一些相关处理,也叫导航守卫,我们这里就通过导航守卫实现一些类似身份认证的辅助功能。 修改router/index.js文件,添加导航守卫,在每次路由时判断用户会话是否过期。如果登录有效且跳转的是登录界面,就直接路由到主页;如果是非登录页面且会话过期,就跳到登录页面要求登录。 代码如下 router.beforeEach
2023-07-14 · StarBlog前端开发-Vue · 约 1 分钟
StarBlog博客开发笔记:前端相关
markdown编辑器 v-md-editor,http://ckang1229.gitee.io/vue-markdown-editor/zh/ 在我的项目里最大化有问题,后面我自己搭配vuex实现了 优点是界面比较好,TOC可以放在左边 其他尝试 vue-element-admin整合的markdown编辑器:https://panjiachen.github.io/vue-element-a
2023-07-14 · StarBlog前端开发-Vue · 约 1 分钟
讨论
共 0 条加载评论中…