Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

vue包括一个模板(template)、一个脚本(script)和一个样式(style)部分。

基础知识

  1. 模板(template)部分

    该部分定义了界面的布局和交互元素。其中包括:

    • 标题和按钮:展示标题 “学生管理系统” 和若干按钮,用于获取学生信息、添加学生信息、登录和注册。
    • 弹窗(Dialog):使用 Element UI 的 el-dialog 组件实现了添加学生信息、登录和注册的弹窗界面。每个弹窗包含一些表单项,用户可以输入信息并进行相应操作。
    • 学生列表:展示学生信息的表格,通过 v-for 循环渲染 Student 组件,显示学生的各个字段。
    • 分页按钮:使用 Element UI 的 el-button 组件实现了上一页和下一页的分页功能。
  2. 脚本(script)部分

    在这里,组件的逻辑部分被定义。它包括以下内容:

    • data:定义了一些数据属性,包括用户登录和注册的相关信息、当前页码、每页显示的学生数、学生列表数据、添加学生弹窗的状态等。
    • computed:定义了 displayedStudents 计算属性,用于根据当前页码和每页显示数来计算显示的学生列表。
    • methods:包含了各种方法,例如获取学生信息、添加学生、翻页、登录、注册等。
  3. 样式(style)部分

    该部分定义了组件的样式。其中使用了一些 CSS 类来设置标题、按钮、表单等的样式。

    关于外部工具的引入

    我们用npm引入

    安装element-ui包,在终端执行以下命令

    1
    npm i element-ui

    使用 Element UI 提供的样式类和自定义样式来美化界面。

    安装bootstrap

    1
    npm install bootstrap@5.2.0-beta1

    安装axios

    1
    npm i axios
    基本使用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import axios from 'axios'

    axios.get('http://localhost:8000/students').then(
    response => {
    console.log(response.data);
    },
    error =>{
    console.log(error.massage);
    }
    )

项目基本结构

app.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<template>
<div class="App">
<h1 class="title">学生管理系统</h1>
<div class="button-container">
<el-button type="primary" @click="getStudents">获取学生信息</el-button>
<el-button type="warning" @click="dialogVisible = true">添加学生信息</el-button>
<el-button type="info" @click="dialogVisiblelogin = true">登 录</el-button>
<el-button type="danger" @click="dialogVisibleregister = true">注 册</el-button>
</div>

<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose">
<div>添加学生信息</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="addStudent">添加</el-button>
</span>
<div>
<div><span>学号:</span><input v-model="newStudent.number" /></div>
<div><span>姓名:</span><input v-model="newStudent.name" /></div>
<div><span>年龄:</span><input v-model="newStudent.age" /></div>
<div><span>语文:</span><input v-model="newStudent.chi" /></div>
<div><span>数学:</span><input v-model="newStudent.math" /></div>
<div><span>英语:</span><input v-model="newStudent.eng" /></div>
</div>
</el-dialog>

<el-dialog
title="登录"
:visible.sync="dialogVisiblelogin"
width="30%"
:before-close="handleClose">
<div class="form-group">
<div class="form-item">
<span>用户名:</span><input type="text" v-model="user_for_login.username">
</div>
<div class="form-item">
<span>密 码:</span><input type="password" v-model="user_for_login.password">
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisiblelogin = false">取 消</el-button>
<el-button type="primary" @click="login">登 录</el-button>
</span>
</el-dialog>

<el-dialog
title="注册"
:visible.sync="dialogVisibleregister"
width="30%"
:before-close="handleClose">
<div class="form-group">
<div class="form-item">
<span>用户名:</span><input type="text" v-model="user_for_register.username">
</div>
<div class="form-item">
<span>密 码:</span><input type="password" v-model="user_for_register.password">
</div>
<div class="form-item">
<span>确认密码:</span><input type="password" v-model="user_for_register.confirmPassword">
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisibleregister = false">取 消</el-button>
<el-button type="primary" @click="register">注 册</el-button>
</span>
</el-dialog>


<div class="col-8 offset-2">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">学号</th>
<th scope="col">姓名</th>
<th scope="col">年龄</th>
<th scope="col">语文</th>
<th scope="col">数学</th>
<th scope="col">英语</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<Student v-for="stu in displayedStudents" :key="stu.id" :student="stu" />
</tbody>
</table>
<div class="text-center">
<el-button-group>
<el-button type="primary" icon="el-icon-arrow-left" @click="prevPage">上一页</el-button>
<el-button type="primary" @click="nextPage">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>
</el-button-group>
</div>
</div>
</div>
</template>

<script>
import axios from 'axios'
import Student from "./components/Student.vue"

export default {
name: 'App',
components: {
Student
},
data() {
return {
user_for_login:{
username:"",
password:"",
},
user_for_register:{
username:"",
password:"",
confirmPassword:"",
},
page: 1,
pageSize: 10,
students: [],
dialogVisible: false,
dialogVisiblelogin:false,
dialogVisibleregister:false,
newStudent: {
number: "",
name: "",
age: "",
chi: "",
math: "",
eng: ""
}
}
},
computed: {
displayedStudents() {
const startIdx = (this.page - 1) * this.pageSize;
return this.students.slice(startIdx, startIdx + this.pageSize);
}
},
methods: {
getStudents() {
if (sessionStorage.getItem("isLogined") == "true") {
axios({
url: "http://localhost:8080/students",
method: 'GET',
}).then(res => {
console.log(res.data);
this.students = res.data; // 更新 students 数据
})
}
else
{
this.$alert("请先登录");
}
},
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
})
.catch(_ => {});
},
addStudent() {
axios({
url: "http://localhost:8080/insert",
method: 'POST',
data: this.newStudent
});
this.dialogVisible = false;
},
prevPage() {
if (this.page > 1) {
this.page--;
}
},
nextPage() {
const maxPage = Math.ceil(this.students.length / this.pageSize);
if (this.page < maxPage) {
this.page++;
}
},
login(){
axios({
url : "http://localhost:8080/login",
data:this.user_for_login,
method:"POST"
}).then(res =>{
console.log(res.data);
localStorage.setItem("login",res.data)
if(res.data == "1"){
sessionStorage.setItem("isLogined","true");

}
else {
alert("用户名密码错误")
}

})
this.dialogVisiblelogin = false
},
register(){
axios({
url : "http://localhost:8080/register",
data:this.user_for_register,
method:"POST"
})
this.$alert("注册成功")
}
}
};

</script>

<style>
.title {
text-align: center;
}

.col-8.offset-2 {
margin-top: 20px;
}

.button-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.form-group {
display: flex;
flex-direction: column;
}

.form-item {
display: flex;
align-items: center;
margin-bottom: 10px;
}

.form-item span {
min-width: 100px; /* Adjust the width as needed */
display: inline-block;
}
</style>

student.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<template>
<div class="App">
<h1 class="title">学生管理系统</h1>
<div class="button-container">
<el-button type="primary" @click="getStudents">获取学生信息</el-button>
<el-button type="warning" @click="dialogVisible = true">添加学生信息</el-button>
<el-button type="info" @click="dialogVisiblelogin = true">登 录</el-button>
<el-button type="danger" @click="dialogVisibleregister = true">注 册</el-button>
</div>

<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose">
<div>添加学生信息</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="addStudent">添加</el-button>
</span>
<div>
<div><span>学号:</span><input v-model="newStudent.number" /></div>
<div><span>姓名:</span><input v-model="newStudent.name" /></div>
<div><span>年龄:</span><input v-model="newStudent.age" /></div>
<div><span>语文:</span><input v-model="newStudent.chi" /></div>
<div><span>数学:</span><input v-model="newStudent.math" /></div>
<div><span>英语:</span><input v-model="newStudent.eng" /></div>
</div>
</el-dialog>

<el-dialog
title="登录"
:visible.sync="dialogVisiblelogin"
width="30%"
:before-close="handleClose">
<div class="form-group">
<div class="form-item">
<span>用户名:</span><input type="text" v-model="user_for_login.username">
</div>
<div class="form-item">
<span>密 码:</span><input type="password" v-model="user_for_login.password">
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisiblelogin = false">取 消</el-button>
<el-button type="primary" @click="login">登 录</el-button>
</span>
</el-dialog>

<el-dialog
title="注册"
:visible.sync="dialogVisibleregister"
width="30%"
:before-close="handleClose">
<div class="form-group">
<div class="form-item">
<span>用户名:</span><input type="text" v-model="user_for_register.username">
</div>
<div class="form-item">
<span>密 码:</span><input type="password" v-model="user_for_register.password">
</div>
<div class="form-item">
<span>确认密码:</span><input type="password" v-model="user_for_register.confirmPassword">
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisibleregister = false">取 消</el-button>
<el-button type="primary" @click="register">注 册</el-button>
</span>
</el-dialog>


<div class="col-8 offset-2">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">学号</th>
<th scope="col">姓名</th>
<th scope="col">年龄</th>
<th scope="col">语文</th>
<th scope="col">数学</th>
<th scope="col">英语</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<Student v-for="stu in displayedStudents" :key="stu.id" :student="stu" />
</tbody>
</table>
<div class="text-center">
<el-button-group>
<el-button type="primary" icon="el-icon-arrow-left" @click="prevPage">上一页</el-button>
<el-button type="primary" @click="nextPage">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>
</el-button-group>
</div>
</div>
</div>
</template>

<script>
import axios from 'axios'
import Student from "./components/Student.vue"

export default {
name: 'App',
components: {
Student
},
data() {
return {
user_for_login:{
username:"",
password:"",
},
user_for_register:{
username:"",
password:"",
confirmPassword:"",
},
page: 1,
pageSize: 10,
students: [],
dialogVisible: false,
dialogVisiblelogin:false,
dialogVisibleregister:false,
newStudent: {
number: "",
name: "",
age: "",
chi: "",
math: "",
eng: ""
}
}
},
computed: {
displayedStudents() {
const startIdx = (this.page - 1) * this.pageSize;
return this.students.slice(startIdx, startIdx + this.pageSize);
}
},
methods: {
getStudents() {
if (sessionStorage.getItem("isLogined") == "true") {
axios({
url: "http://localhost:8080/students",
method: 'GET',
}).then(res => {
console.log(res.data);
this.students = res.data; // 更新 students 数据
})
}
else
{
this.$alert("请先登录");
}
},
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
})
.catch(_ => {});
},
addStudent() {
axios({
url: "http://localhost:8080/insert",
method: 'POST',
data: this.newStudent
});
this.dialogVisible = false;
},
prevPage() {
if (this.page > 1) {
this.page--;
}
},
nextPage() {
const maxPage = Math.ceil(this.students.length / this.pageSize);
if (this.page < maxPage) {
this.page++;
}
},
login(){
axios({
url : "http://localhost:8080/login",
data:this.user_for_login,
method:"POST"
}).then(res =>{
console.log(res.data);
localStorage.setItem("login",res.data)
if(res.data == "1"){
sessionStorage.setItem("isLogined","true");

}
else {
alert("用户名密码错误")
}

})
this.dialogVisiblelogin = false
},
register(){
axios({
url : "http://localhost:8080/register",
data:this.user_for_register,
method:"POST"
})
this.$alert("注册成功")
}
}
};

</script>

<style>
.title {
text-align: center;
}

.col-8.offset-2 {
margin-top: 20px;
}

.button-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.form-group {
display: flex;
flex-direction: column;
}

.form-item {
display: flex;
align-items: center;
margin-bottom: 10px;
}

.form-item span {
min-width: 100px; /* Adjust the width as needed */
display: inline-block;
}
</style>

** 样式设计**

使用 Element UI 提供的样式类和自定义样式来美化界面。

9 部署和测试

部署前端应用到服务器,访问页面并测试各项功能。

优化和扩展

  • 使用 Vue Router 实现页面路由切换。
  • 使用 Vuex 进行状态管理。
  • 实现分页功能。
  • 增加表单验证等。

** 注意事项**

  • 遇到跨域问题,可以配置代理或后端设置 CORS。
  • 错误处理和状态提示。

参考资料

评论