目录
3.3、案例:基于Vue及Axios完成数据的动态加载展示编辑
3.2、Vue的组件(template\script\style)
[2、Vue Router](#2、Vue Router)
3、案例:通过Vue的路由VueRouter完成左侧菜单栏点击切换效果
[2.1、将 dist 目录下的文件复制到 nginx 安装目录](#2.1、将 dist 目录下的文件复制到 nginx 安装目录)
一、Ajax
![](https://file.jishuzhan.net/article/1760902608232386562/5e67282815272bf536b0fda185b7d816.webp)
1、同步与异步![](https://file.jishuzhan.net/article/1760902608232386562/f2d8b61889bc9f42143363850d168de2.webp)
2、原生Ajax(繁琐)![](https://file.jishuzhan.net/article/1760902608232386562/d78882bb123c33000a89386926dd2541.webp)
代码可参考W3School中的参考手册
效果:(查看所有的异步请求,可以点击XHR)
2.1、写一个简易的Ajax
javascript
function ajax(url) {
const p = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true)
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304){
resolve(
JSON.parse(xhr.response)
)
}else{
reject(new Error('Response error'))
}
}
}
xhr.send(null)
})
return p
}
const url = 'text.json'
ajax(url).then(res => console.log(res)).catch(err => console.log(err))
3、Axios(推荐使用)![](https://file.jishuzhan.net/article/1760902608232386562/1d34760369b6e88fea3750573cfd4ed8.webp)
3.1、Axios入门
![](https://file.jishuzhan.net/article/1760902608232386562/f6f305da6343d6bc18464d9c6b0ecc98.webp)
代码示例:
javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>原生Ajax</title>
</head>
<body>
<input type="button" value="获取数据" onclick="getData()">
<div id="div1"></div>
</body>
<script>
function getData(){
//1. 创建XMLHttpRequest
var xmlHttpRequest = new XMLHttpRequest();
//2. 发送异步请求
xmlHttpRequest.open('GET','http://yapi.smart-xwork.cn/mock/169327/emp/list');
xmlHttpRequest.send();//发送请求
//3. 获取服务响应数据
xmlHttpRequest.onreadystatechange = function(){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
document.getElementById('div1').innerHTML = xmlHttpRequest.responseText;
}
}
}
</script>
</html>
3.2、Axios请求方式别名
![](https://file.jishuzhan.net/article/1760902608232386562/15ee3e1c58be902385c37e6ee62f4f0c.webp)
代码示例:
javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax-Axios</title>
<script src="js/axios-0.18.0.js"></script>
</head>
<body>
<input type="button" value="获取数据GET" onclick="get()">
<input type="button" value="删除数据POST" onclick="post()">
</body>
<script>
function get(){
//通过axios发送异步请求-get
// axios({
// method: "get",
// url: "http://yapi.smart-xwork.cn/mock/169327/emp/list"
// }).then(result => {
// console.log(result.data);
// })
axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then(result => {
console.log(result.data);
})
}
function post(){
//通过axios发送异步请求-post
// axios({
// method: "post",
// url: "http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",
// data: "id=1"
// }).then(result => {
// console.log(result.data);
// })
axios.post("http://yapi.smart-xwork.cn/mock/169327/emp/deleteById","id=1").then(result => {
console.log(result.data);
})
}
</script>
</html>
3.3、案例:基于Vue及Axios完成数据的动态加载展示![](https://file.jishuzhan.net/article/1760902608232386562/5a1d974c87bdcd19718cebecf07066b2.webp)
代码:
javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax-Axios-案例</title>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
<table border="1" cellspacing="0" width="60%">
<tr>
<th>编号</th>
<th>姓名</th>
<th>图像</th>
<th>性别</th>
<th>职位</th>
<th>入职日期</th>
<th>最后操作时间</th>
</tr>
<tr align="center" v-for="(emp,index) in emps">
<td>{{index + 1}}</td>
<td>{{emp.name}}</td>
<td>
<img :src="emp.image" width="70px" height="50px">
</td>
<td>
<span v-if="emp.gender == 1">男</span>
<span v-if="emp.gender == 2">女</span>
</td>
<td>{{emp.job}}</td>
<td>{{emp.entrydate}}</td>
<td>{{emp.updatetime}}</td>
</tr>
</table>
</div>
</body>
<script>
new Vue({
el: "#app",
data: {
emps:[]
},
mounted () {
//发送异步请求,加载数据
axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then(result => {
this.emps = result.data.data;
})
}
});
</script>
</html>
二、前后端分离开发
1、前后端开发模式
1.1、前后端混合开发
![](https://file.jishuzhan.net/article/1760902608232386562/0242abc821ae9663d2018f04a19635da.webp)
1.2、前后端分离开发(主流模式)and前后端分离开发流程
![](https://file.jishuzhan.net/article/1760902608232386562/a917477f83c79b2211c6bb892bf33a0f.webp)
2、YAPI(接口文档的管理平台)
YAPI已经暂停使用
三、前端工程化
![](https://file.jishuzhan.net/article/1760902608232386562/3eb5a3b7b1d767272f65d1311b4ae2cd.webp)
1、Vue脚手架环境准备
![](https://file.jishuzhan.net/article/1760902608232386562/bcf5e059995c1c9b64b44d0dbf9d9539.webp)
1.1、安装NodeJS
安装过程可参考:NodeJS 安装文档 .md
2、Vue项目简介
2.1、Vue项目创建
![](https://file.jishuzhan.net/article/1760902608232386562/2a5fb05024753e4800fc7317d1e62b73.webp)
2.2、Vue项目目录结构
![](https://file.jishuzhan.net/article/1760902608232386562/df58231a44db81dfe117ffd72f41f032.webp)
2.3、Vue项目启动(serve)
![](https://file.jishuzhan.net/article/1760902608232386562/11fcb646c3b8d489c2b51fa25d1fd756.webp)
2.4、修改Vue项目端口(vue.config.js)
![](https://file.jishuzhan.net/article/1760902608232386562/40b370a231928ebf96482c11b0950f93.webp)
3、Vue项目开发流程
![](https://file.jishuzhan.net/article/1760902608232386562/a21491fdc656605c3cfa8e5c53419038.webp)
3.1、Vue页面的显示过程
render 是一个函数,它的作用就是将 App 当中定义的视图创建出对应的虚拟DOM 元素,然后挂载到 #app 这个区域
![](https://file.jishuzhan.net/article/1760902608232386562/55acd600d826b91319ecf5f0b315e044.webp)
3.2、Vue的组件(template\script\style)
![](https://file.jishuzhan.net/article/1760902608232386562/a79ff018154ec66bd2af236fcd545afc.webp)
template相当于HTML部分,script就是js部分,style就是css的样式
在Vue项目的开发中,其实main.js以及index.html很少会去操作,主要修改的都是.vue文件
![](https://file.jishuzhan.net/article/1760902608232386562/d7b206a0edf7a05f84e7690fd747c331.webp)
四、Vue组件库Element
![](https://file.jishuzhan.net/article/1760902608232386562/c4a533a8c0f36d872937422a3956df8d.webp)
官方文档:https://element.eleme.cn/#/zh-CN/component/installation
1、什么是Element?
![](https://file.jishuzhan.net/article/1760902608232386562/9aedb7c3f1e1e4fc32588a30871cf251.webp)
为什么要学Element?主要是方便和优美
2、Element快速入门
![](https://file.jishuzhan.net/article/1760902608232386562/e2eded35f41c151f4c2ed0753332cfe6.webp)
2.1、安装ElementUI组件库
javascript
npm install element-ui@2.15.3
会被安装在node_modules目录下
![](https://file.jishuzhan.net/article/1760902608232386562/92c497bc6f0d4c5848431ed40c6a339b.webp)
![](https://file.jishuzhan.net/article/1760902608232386562/2d8d4ca961f33bf09d2b8b8f4e060f5d.webp)
2.2、引入ElementUI组件库
views目录下新建目录(element):![](https://file.jishuzhan.net/article/1760902608232386562/44d61f6452d4197236d04e925f321288.webp)
默认显示根组件(App.vue)中的内容,所以要想显示其它组件,需要在根组件中进行设置:在template中定义element-view标签,下方的import语句就会自动导入
代码
javascript
<template>
<div>
<!-- button按钮 -->
<el-row>
<el-button>默认按钮</el-button>
<el-button type="primary">主要按钮</el-button>
<el-button type="success">成功按钮</el-button>
<el-button type="info">信息按钮</el-button>
<el-button type="warning">警告按钮</el-button>
<el-button type="danger">危险按钮</el-button>
</el-row>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>
![](https://file.jishuzhan.net/article/1760902608232386562/ee1cd9ab5237279c45c8d0a09784af2f.webp)
2.3、定义Vue组件(内容可复制官方文档)
views目录下新建目录(element):
![](https://file.jishuzhan.net/article/1760902608232386562/2d8d4ca961f33bf09d2b8b8f4e060f5d.webp)
javascript
<!-- button按钮 -->
<el-row>
<el-button>默认按钮</el-button>
<el-button type="primary">主要按钮</el-button>
<el-button type="success">成功按钮</el-button>
<el-button type="info">信息按钮</el-button>
<el-button type="warning">警告按钮</el-button>
<el-button type="danger">危险按钮</el-button>
</el-row>
默认显示根组件(App.vue)中的内容,所以要想显示其它组件,需要在根组件中进行设置:在template中定义element-view标签,下方的import语句就会自动导入
效果显示:
3、常见组件
3.1、表格(Table)![](https://file.jishuzhan.net/article/1760902608232386562/e446a1a226eaea413ebd6291836cb202.webp)
使用步骤:
Ⅰ、从官网复制代码到 ElementView.vue :
![](https://file.jishuzhan.net/article/1760902608232386562/6df9e85a74680cfceb6eed4a948b94c1.webp)
javascript
<!-- Table 表格 -->
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"> </el-table-column>
<el-table-column prop="name" label="姓名" width="180"> </el-table-column>
<el-table-column prop="address" label="地址"> </el-table-column>
</el-table>
javascript
tableData: [
{
date: "2016-05-02",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
},
{
date: "2016-05-04",
name: "王小虎",
address: "上海市普陀区金沙江路 1517 弄",
},
{
date: "2016-05-01",
name: "王小虎",
address: "上海市普陀区金沙江路 1519 弄",
},
{
date: "2016-05-03",
name: "王小虎",
address: "上海市普陀区金沙江路 1516 弄",
},
],
![](https://file.jishuzhan.net/article/1760902608232386562/8dba66fbcc679a9570651fb3c29d39e7.webp)
3.2、分页组件(Pagination)
![](https://file.jishuzhan.net/article/1760902608232386562/4d8e71c7e180d25a67d2ebcc585b140b.webp)
javascript
<!-- Pagination 分页 -->
<el-pagination
background
layout="total,sizes,prev, pager, next,jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:total="1000"
>
</el-pagination>
layout属性:
事件:size-change 与 current-change :
javascript
methods: {
handleSizeChange: function (val) {
alert("每页记录数变化" + val);
},
handleCurrentChange: function (val) {
alert("页码发生变化" + val);
}
},
};
![](https://file.jishuzhan.net/article/1760902608232386562/dcd58c6ba6ef7737359e8fbc837481d8.webp)
![](https://file.jishuzhan.net/article/1760902608232386562/a7ac9d56ca10babdc6b5a324447e715e.webp)
3.3、对话框组件(Dialog)
![](https://file.jishuzhan.net/article/1760902608232386562/c745a348342466f3847250cf0e46cdb6.webp)
javascript
<!-- Dialog对话框 -->
<!-- Table -->
<el-button type="text" @click="dialogTableVisible = true"
>打开嵌套表格的 Dialog</el-button
>
<el-dialog title="收货地址" :visible.sync="dialogTableVisible">
<el-table :data="gridData">
<el-table-column
property="date"
label="日期"
width="150"
></el-table-column>
<el-table-column
property="name"
label="姓名"
width="200"
></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
</el-dialog>
数据模型:
javascript
gridData: [
{
date: "2016-05-02",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
},
{
date: "2016-05-04",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
},
{
date: "2016-05-01",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
},
{
date: "2016-05-03",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
},
],
dialogTableVisible: false,
3.4、表单组件(Form)![](https://file.jishuzhan.net/article/1760902608232386562/19c23ed62e55cfa4b813882691e527f2.webp)
javascript
<!-- Dialog对话框-Form表单 -->
<el-button type="text" @click="dialogFormVisible = true"
>打开嵌套Form的Dialog Dialog</el-button
>
<el-dialog title="Form表单" :visible.sync="dialogFormVisible">
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="活动名称">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="活动区域">
<el-select v-model="form.region" placeholder="请选择活动区域">
<el-option label="区域一" value="shanghai"></el-option>
<el-option label="区域二" value="beijing"></el-option>
</el-select>
</el-form-item>
<el-form-item label="活动时间">
<el-col :span="11">
<el-date-picker
type="date"
placeholder="选择日期"
v-model="form.date1"
style="width: 100%"
></el-date-picker>
</el-col>
<el-col class="line" :span="2">-</el-col>
<el-col :span="11">
<el-time-picker
placeholder="选择时间"
v-model="form.date2"
style="width: 100%"
></el-time-picker>
</el-col>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">提交</el-button>
<el-button>取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
数据模型
javascript
form: {
name: '',
region: '',
date1: '',
date2: '',
},
方法:
通过JSON.stringify()将对象转为字符串
javascript
onSubmit :function(){
//JSON>stringify json对象转字符串
alert(JSON.stringify(this.form))
}
效果:
4、案例:根据页面原型完成员工管理页面的开发
![](https://file.jishuzhan.net/article/1760902608232386562/ba5a4db18dacef81c0efd6eba80fe15a.webp)
实现步骤:
![](https://file.jishuzhan.net/article/1760902608232386562/e34d473e82990cd34e672f38deb7a1f9.webp)
4.1、创建页面,并完成页面的整体布局规划
创建EmpView.vue文件,并在根组件中进行声明和导入
![](https://file.jishuzhan.net/article/1760902608232386562/3a99f93090d740b82ea8cc5fc8615707.webp)
待续~
4.2、布局中各个组件的实现
4.3、列表数据的异常加载与渲染展示
![](https://file.jishuzhan.net/article/1760902608232386562/8e4655414c21e6c2cdfd930497c3351d.webp)
安装Axios
javascript
npm install axios
![](https://file.jishuzhan.net/article/1760902608232386562/ccd8982517053ca662d4dad9d86687cf.webp)
导入Axios:
在EmpView.vue文件中导入:
javascript
import axios from "axios";
发送异步请求:
yapi不可以用了,详见ajax地址访问不了-解决方案文件夹
javascript
mounted() {
//发送异步请求,获取数据
axios.get("http://localhost:10010/emp/list").then((result) => {
this.tableData = result.data.data;
});
},
效果展示:
![](https://file.jishuzhan.net/article/1760902608232386562/d4739ff6479d059894ceef5d54d81c84.webp)
解决图片与性别展示(使用插槽 slot-scope)
row指代这一行所有的字段值
效果展示
五、Vue路由
1、前端路由
![](https://file.jishuzhan.net/article/1760902608232386562/d66f1a8f483de46b3e66549188dbf176.webp)
2、Vue Router
![](https://file.jishuzhan.net/article/1760902608232386562/a55631e85b6b003c9fd15c3ddbefce0c.webp)
2.1、安装vue-router
安装**(创建Vue项目时已选择)**
javascript
npm install vue-router@3.5.1
![](https://file.jishuzhan.net/article/1760902608232386562/3e79d9d4f99e296b9b481c0e95b2dcb7.webp)
2.2、定义路由配置信息(index.js)
![](https://file.jishuzhan.net/article/1760902608232386562/618c30a5f927f4ddb87dad24cb4d5b3b.webp)
如果你要访问的是/emp路径,那么你要导入的就是EmpView.vue组件,/dept同理:
![](https://file.jishuzhan.net/article/1760902608232386562/8aed2afa2851cee7f1700452eb175f2d.webp)
3、案例:通过Vue的路由VueRouter完成左侧菜单栏点击切换效果
示例代码:
在组件的template中的对应位置添加<router-link>标签:
javascript
<el-container style="height: 700px; border: 1px solid #eee">
<el-header style="font-size: 40px; background-color: rgb(238, 241, 246)"
>tlias 智能学习辅助系统</el-header
>
<el-container>
<el-aside width="230px" style="border: 1px solid #eee">
<el-menu :default-openeds="['1', '3']">
<el-submenu index="1">
<template slot="title"
><i class="el-icon-message"></i>系统信息管理</template
>
<el-menu-item index="1-1">
<router-link to="/dept">部门管理</router-link>
</el-menu-item>
<el-menu-item index="1-2">
<router-link to="/emp">员工管理</router-link>
</el-menu-item>
</el-submenu>
</el-menu>
</el-aside>
在根组件中添加对应的<router-view>标签:
![](https://file.jishuzhan.net/article/1760902608232386562/7a2a2ab48bd508b1ae897bc093917d48.webp)
4、Bug:默认访问路由路径为/
|--------------------------------------------------------------------------------|
| 问题:由于默认的访问路由路径为/,但我们在index.js中配置的路径只有两个,并不存在/,那么怎么解决这个问题呢?如果你访问/是找不到对应的组件的 |
| 解决办法:很简单!没有就加上 |
javascript
const routes = [
{
path: '/emp',
name: 'emp',
component: () => import('../views/tlias/EmpView.vue')
},
{
path: '/dept',
name: 'dept',
component: () => import('../views/tlias/DeptView.vue')
},
{
path: '/',
redirect: '/dept'
}
]
六、打包部署
1、如何打包?
![](https://file.jishuzhan.net/article/1760902608232386562/df4b209d35cd3e618c3a22ff4af430d6.webp)
直接运行build 即可打包,打包生成的文件将放入dist
效果展示
![](https://file.jishuzhan.net/article/1760902608232386562/0a53b0fe5a234e1f521b96bc0476bf00.webp)
打包后的文件:
![](https://file.jishuzhan.net/article/1760902608232386562/0637dd73edee781a868d3632ef0da2e7.webp)
2、如何部署?(Nginx)
![](https://file.jishuzhan.net/article/1760902608232386562/9656503e685504d0694636113e5ee18b.webp)
2.1、将 dist 目录下的文件复制到 nginx 安装目录
![](https://file.jishuzhan.net/article/1760902608232386562/263b20399348294073c0dde10f5e2512.webp)
2.2、启动nginx.exe![](https://file.jishuzhan.net/article/1760902608232386562/cf7f31293116aab28f9d3c84b3f63a1e.webp)
更换 nginx 的端口号
config目录 -> nginx.conf:
找到默认端口,将其改为90:
![](https://file.jishuzhan.net/article/1760902608232386562/c0854dcfd229ce8d201ba3e28bbd23cb.webp)
访问项目
2.3、扩展:反向代理、负载均衡
反向代理可参考: 实现nginx反向代理(附nginx教程)
负载均衡可参考: Nginx如何优雅的实现负载均衡