Well, we have already written the back-end service before
, and then we will continue to write our front-end project. The previous article SSMP integration case (1) Build the Spring Boot Vue MySql project environment. We have built the front-end project environment.
We open it in terminal input
npm i axios@0.21.0
Introduce axios, which is a third-party plug-in specially used to send requests.
Be sure to pay attention to the version after @, which is quite important.
Then we introduce Sass
Execute in terminal
npm install sass-loader node-sass@4.14.1 --save-dev
Then our Sass came in
Then we create a folder called utils in the project src directory
and create a request.js request.js in this folder.
The reference code is as follows
import axios from 'axios';
//在vue原型链中封装post请求方法
function createService () {
// 创建一个 axios 实例
const service = axios.create()
// 请求拦截
service.interceptors.request.use(
//发送成功
config => {
//可在此配置请求头信息
config.headers.Authorization = `Bearer`;
return config
},
//发送失败
error => {
return error
}
)
//响应拦截
service.interceptors.response.use(
//请求成功
response => {
//抛出返回值
return response&&response.data?response.data:{
state: 500,message: "系统异常"};
},
//请求失败
(error) => {
//返回错误信息
return error;
}
)
//抛出对象
return service
}
function createRequestFunction (service) {
return function (config) {
const configDefault = {
headers: {
'Content-Type':'application/json'
},
timeout: 100000,
baseURL: "",
data: {
}
}
return service(Object.assign(configDefault, config))
}
}
const service = createService()
export const request = createRequestFunction(service);
We made a secondary package of axios
and created a bookApi.js under the src directory under the api folder.
The reference code is as follows
import {
request} from '@/utils/request'
export function get(params){
return request({
url:`/books`,
method:'get',
params:params,
})
}
Here we define the function request method of requesting http://localhost/books as get
, but such a direct request must be cross-domain
We find vue.config.js in the root directory of the project
and add it
devServer: {
proxy: {
"/books": {
target: 'http://localhost/',
ws: true,
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
},
}
}
It means that when books are matched, we directly proxy to http://localhost/ address,
just like
/books, after proxy processing, it is http://localhost/books
We write the App.vue component code under src as follows
<template>
<div class="app">
</div>
</template>
<script>
import {
getAll } from "@/api/bookApi.js"
export default {
name: 'App',
data() {
return {
}
},
mounted() {
getAll({
}).then(res => {
console.log(res);
})
}
}
</script>
We called getAll in the mounted life cycle to send the request and accept the returned result.
We can see that the data appears correctly when we run the project
Then I directly write the App component code as follows
<template>
<div class="app">
<h1 class = "title">图书管理</h1>
<el-form
:inline="true"
:model="formData"
class="demo-form-inline"
size="small"
>
<el-form-item>
<el-input v-model="formData.type" placeholder="圈书类别"></el-input>
</el-form-item>
<el-form-item>
<el-input v-model="formData.name" placeholder="图书名称"></el-input>
</el-form-item>
<el-form-item>
<el-input v-model="formData.description" placeholder="图书描述"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="queryGet">查询</el-button>
<el-button type="primary" @click="AddBook">新建</el-button>
</el-form-item>
</el-form>
<el-table
:data="bookList"
style="width: 100%"
height="calc(100vh - 200px)"
>
<el-table-column
prop="name"
label="图书名称"
>
</el-table-column>
<el-table-column
label="类别"
width="180">
<template slot-scope="scope">
{
{ typeIn[scope.row.type] }}
</template>
</el-table-column>
<el-table-column
prop="description"
label="描述">
</el-table-column>
</el-table>
<div class = "pages">
<el-pagination
background
:page-size="page.pageSize"
:page-count="page.pageCount"
layout="prev, pager, next"
:total="total">
</el-pagination>
</div>
</div>
</template>
<script>
import {
getAll } from "@/api/bookApi.js"
export default {
name: 'App',
data() {
return {
total: 0,
page: {
pageSize: 10,
pageCount: 1
},
formData: {
type: "",
name: "",
description: "",
},
bookList: [],
typeIn: {
0: "知识科普",
1: "休闲图书",
2: "官方记录",
3: "典藏"
}
}
},
methods: {
queryGet() {
},
AddBook() {
},
getAll() {
getAll({
}).then(res => {
if(res.state == 200) {
this.bookList = res.data;
}else{
this.$message.error(res.message);
}
})
}
},
mounted() {
this.getAll();
}
}
</script>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
</style>
<style lang="scss" scoped>
.app{
width: 100vw;
height: 100vh;
padding: 0 15px;
.title{
padding: 12px 0;
border-bottom: 3px solid rgb(88,132,159);
margin-bottom: 12px;
}
.pages{
display: flex;
justify-content:flex-end;
}
}
</style>
Our interface is almost out of a prototype