4.前端环境搭建
前端环境搭建
一、vue-element-admin
- 解压文件到项目工作区
- 根据package.json下载需要的依赖
npm install
- 然后运行
npm run dev
二、登录改造
..\src\store\modules\user.js
1 | Login({ commit }, userInfo) { |
三、框架开发过程
- 添加路由
- 设置要跳转的页面路径
- 在api文件中创建js文件,定义接口路径
- 在页面中引入js文件,使用axios进行接口调用,把接口返回数据在页面显示
四、医院设置前端开发
添加医院设置路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19path: '/hospSet',
component: Layout,
redirect: '/hospSet/list',
name: '医院设置管理',
meta: { title: '医院设置管理', icon: 'example' },
children: [
{
path: 'list',
name: '医院设置列表',
component: () => import('@/views/hospset/list'),
meta: { title: '医院设置列表', icon: 'table' }
},
{
path: 'add',
name: '医院设置添加',
component: () => import('@/views/hospset/add'),
meta: { title: '医院设置添加', icon: 'tree' }
}
]创建跳转页面,设置跳转路径
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<template>
<div class="app-container">
医院设置添加
<el-form label-width="120px">
<el-form-item label="医院名称">
<el-input v-model="hospitalSet.hosname"/>
</el-form-item>
<el-form-item label="医院编号">
<el-input v-model="hospitalSet.hoscode"/>
</el-form-item>
<el-form-item label="api基础路径">
<el-input v-model="hospitalSet.apiUrl"/>
</el-form-item>
<el-form-item label="联系人姓名">
<el-input v-model="hospitalSet.contactsName"/>
</el-form-item>
<el-form-item label="联系人手机">
<el-input v-model="hospitalSet.contactsPhone"/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="saveOrUpdate">保存</el-button>
</el-form-item>
</el-form>
</div>
</template>在api中创建js文件
1
2
3
4
5
6
7
8
9export default {
//医院设置列表
getHospSetList(current,limit,searchObj) {
return request ({
url: `/admin/hosp/hospitalSet/findPageHospSet/${current}/${limit}`,
method: 'post',
data: searchObj //使用json形式进行传递
})
}//引入接口定义的js文件 import hospset from '@/api/hospset' export default { //定义变量和初始值 data() { return { current:1, //当前页 limit:3, //每页显示记录数 searchObj:{}, //条件封装对象 list:[], //每页数据集合 total:0, //总记录数 multipleSelection: [] // 批量选择中选择的记录列表 } }, created() { //在页面渲染之前执行 //一般调用methods定义的方法,得到数据 this.getList() }, methods: {//定义方法,进行请求接口调用 //医院设置列表 getList(page=1) { //添加当前页参数 this.current = page hospset.getHospSetList(this.current,this.limit,this.searchObj) .then(response => { //请求成功 response是接口返回数据 //返回集合赋值list this.list = response.data.records //总记录数 this.total = response.data.total }) .catch(error => {//请求失败 console.log(error) }) }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
**跨域问题**:三个地方,任何一个不相同都会产生跨域问题
1. 访问协议:http https
2. 访问地址:192.168.1.1 172.11.1.1
3. 端口号:9528 8201
**解决方案**:在controller中加上`@CrossOrigin`注解
**分页操作**:
```vue
<!-- 分页 -->
<el-pagination
:current-page="current"
:page-size="limit"
:total="total"
style="padding: 30px 0; text-align: center;"
layout="total, prev, pager, next, jumper"
@current-change="getList"/>
删除医院
1 | //删除医院设置的方法 |
批量删除
1 | //批量删除 |
判断用户是否点击选择::disabled="multipleSelection.length === 0"
选择其他页时,之前页选的的框依然存在::reserve-selection="true"
锁定
使用el-switch实现:
1 | <el-switch v-model="scope.row.status" :active-value="1" |
1 | //锁定和取消锁定 |
添加医院设置
1 | //添加 |
修改
隐藏路由:
1 | { |
1 | <router-link :to="'/hospSet/edit/'+scope.row.id"> |
1 | //获取路由id值 |
1 | //根据id查询 |
组件重用问题
问题:vue-router导航切换时,如果两个路由都渲染同一个组件,组建的生命周期方法(created或者mounted)不会被再次调用,组件会被重用,显示上一个路由渲染出来的自建
解决方法:可以简单的在router-view上加一个唯一的key,来保证路由切换时都会重新触发生命周期方法,确保组件被重新初始化。
在src\views\layout\components\AppMain.vue
中
1 | <router-view :key="key"></router-view> |
1 | computed: { |