1.axios模块注意事项

  1. // 查询工号是否重复
  2. export function getEmployeeCheckUserCode(data) {
  3. return request({
  4. url: 'employee-user/v1/emp-user/code',
  5. method: 'get',
  6. params: data
  7. })
  8. }
  9. getEmployeeCheckUserCode({ code: value, userId: this.userId }).then(res => {
  10. if (res.data.msg === '工号可用') {
  11. callback()
  12. } else {
  13. callback('工号已存在')
  14. }
  15. })

2.0 数组CRUD

1.业务场景删除指定内容,

  1. <el-tag
  2. v-for="(tag, index) in deptData"
  3. :key="index"
  4. closable
  5. @close="handleCloseDept(tag)">
  6. {{ tag.deptName }}
  7. </el-tag>
  8. // 删除部门tag
  9. handleCloseDept(tag) {
  10. // 通过key值的变动强制刷新组件
  11. this.deptData.splice(this.deptData.indexOf(tag), 1)
  12. },

2.0 el-tags和el-tree树形结构的综合运用情况

3 el-table设置表格高度自适应

  1. <el-table ref="table" :data="tableData" :height="tableHeight"></el-table>
  1. export default {
  2. data(){
  3. return {
  4. tableHeight: 50,
  5. tableData: []
  6. }
  7. },
  8. mounted:function(){
  9. this.$nextTick(function () {
  10. this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 50;
  11. // 监听窗口大小变化
  12. let self = this;
  13. window.onresize = function() {
  14. self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - 50
  15. }
  16. })
  17. //this.$refs.table.$el.offsetTop:表格距离浏览器的高度
  18.      //50表示你想要调整的表格距离底部的高度(你可以自己随意调整),因为我们一般都有放分页组件的,所以需要给它留一个高度 
  19. }
  20. }

4.vue路由跳转打开新一页

  1. outsideLink () {
  2. let {href}= this.$router.resolve({
  3. path: "/newLinkPage",
  4. });
  5. window.open(href, '_blank');
  6. }

5.0 el-table全选和取消全选2种方式

5.1 自定义的表格模式

  • 自定义封装列表的选项框勾选,使用自定义方式完成全选或者取消,选择个别状态为isIndeterminate 等待状态。
  1. <el-table-column width="55">
  2. <template slot-scope="scope">
  3. <el-checkbox v-model="scope.row.isCheck" @change="handleCheck($event, scope.row)"/>
  4. </template>
  5. </el-table-column>
  1. table点击点击选择

    • 第一步先判断点击的数组和当前传进来的数据是否存在,存在返回下标没有则-1
    • 如果不存在空的则把选择的内容添加到中间数组 this.checkList 则表示当前是选择点击勾选状态
    • 存在数据并且返回当前数据下标则 haveAny !== -1进行下标的删除等于 取消勾选状态取消选择
    • this.isIndeterminate通过当前点击的长度和输入的中间数组长度判断若不相等则为等待状态表示false(indeterminate 属性用以表示 checkbox 的不确定状态,一般用于实现全选的效果)
    • checkAll全部选择状态,table表格全部选择表示当前状态
  1. handleCheck(val, item) {
  2. const haveAny = this.checkList.findIndex((citem) => {
  3. return citem.orderId === item.orderId
  4. })
  5. if (val) {
  6. if (haveAny === -1) {
  7. this.checkList.push(item)
  8. }
  9. } else {
  10. if (haveAny !== -1) {
  11. this.checkList.splice(haveAny, 1)
  12. }
  13. }
  14. this.isIndeterminate = this.checkList.length > 0 && this.checkList.length < this.tableData.length
  15. this.checkAll = this.checkList.length > 0 && this.checkList.length === this.tableData.length
  16. console.log(this.checkList)
  17. },
  1. 自定义checkbox点击实现table自定义的 checkbox全部选择
  • 先让 this.isIndeterminate状态隐藏
  • 判断 this.tableData 内容使用 forEach遍历完成 开关状态
  • 并且完成赋值修改最后的选择状态
  1. handleCheckAllChange(val) {
  2. this.isIndeterminate = false
  3. if (val) {
  4. this.tableData.forEach(item => {
  5. item.isCheck = true
  6. })
  7. this.checkList = JSON.parse(JSON.stringify(this.tableData))
  8. } else {
  9. this.tableData.forEach(item => {
  10. item.isCheck = false
  11. })
  12. this.checkList = []
  13. }
  14. },

5.2 自定义的表格模式

  1. <el-table @selection-change="handleSelectionChange"
  2. <el-table-column type="selection" width="55"/>
  3. <el-table/>
  1. 自定义全选模式

    • 完成中间数组的赋值插入,根据插入的数据的长度和 this.tableData.length 判断实现对自定义checkbox的状态切换修改
  1. handleSelectionChange(val) {
  2. this.checkList = val
  3. this.checkList.length === this.tableData.length ? this.checkAll = true : this.checkAll = false
  4. console.log(this.checkList)
  5. },
  1. 点击选项
  • 第一步先判断点击的数组和当前传进来的数据是否存在,存在返回下标没有则-1
  • 如果不存在空的则把选择的内容添加到中间数组 this.checkList 则表示当前是选择点击勾选状态
  • 存在数据并且返回当前数据下标则 haveAny !== -1进行下标的删除等于 取消勾选状态取消选择
  • this.isIndeterminate通过当前点击的长度和输入的中间数组长度判断若不相等则为等待状态表示false(indeterminate 属性用以表示 checkbox 的不确定状态,一般用于实现全选的效果)
  • checkAll全部选择状态,table表格全部选择表示当前状态
  1. // 点击选项
  2. handleCheck(val, item) {
  3. const haveAny = this.checkList.findIndex((citem) => {
  4. return citem.userId === item.userId
  5. })
  6. if (val) {
  7. if (haveAny === -1) {
  8. this.checkList.push(item)
  9. }
  10. } else {
  11. if (haveAny !== -1) {
  12. this.checkList.splice(haveAny, 1)
  13. }
  14. }
  15. this.isIndeterminate = this.checkList.length > 0 && this.checkList.length < this.tableData.length
  16. this.checkAll = this.checkList.length > 0 && this.checkList.length === this.tableData.length
  17. },
  1. 当页全选模式
  1. // 当页全选
  2. handleCheckAllChange(val) {
  3. console.log(val)
  4. this.isIndeterminate = false
  5. if (val) {
  6. this.tableData.forEach(item => {
  7. const isCheck = item.isCheck = true
  8. this.$refs.table.toggleRowSelection(item, isCheck)
  9. })
  10. } else {
  11. this.tableData.forEach(item => {
  12. item.isCheck = false
  13. this.$refs.table.clearSelection()
  14. })
  15. this.checkList = []
  16. }
  17. },

6.0 树形结构权限树形(难点)

  1. <!-- 树形结构 -->
  2. <el-tree
  3. ref="tree"
  4. :data="treeData"
  5. :expand-on-click-node="false"
  6. :props="defaultProps"
  7. :default-expanded-keys="treeNnfoldArray"
  8. :highlight-current="isHighlight"
  9. node-key="deptId"
  10. @node-click="setUserData"
  11. @node-expand="nodeExpand"
  12. @node-collapse="nodeCollapse"
  13. >
  14. <span slot-scope="{ node, data }" class="custom-tree-node">
  15. <span style="margin-right: 6px">
  16. <img src="./img/Process.png" >
  17. </span>
  18. <span style="margin-right: 6px">{{ node.label }}</span>
  19. </span>
  20. </el-tree>

7.0 Popover 弹出框嵌套只显示一个处理方案

  1. if (this.cacheDataItem) {
  2. this.cacheDataItem.visible = false
  3. }

8.0 绝对定位布局

  1. <el-drawer
  2. title="配置权限"
  3. :visible.sync="drawer"
  4. direction="rtl"
  5. :before-close="handleClose"
  6. size="40%">
  7. <div style="position: absolute;top:52px;left: 0;right: 0;bottom: 0;">
  8. <div style="position: absolute;top: 0;left: 0;right: 0;bottom: 60px;overflow-y: auto;">
  9. <el-tree
  10. :data="ruleList"
  11. show-checkbox
  12. node-key="id"
  13. default-expand-all
  14. :default-checked-keys="checkedKeys"
  15. :props="defaultProps"
  16. :check-strictly="true"
  17. @check="check">
  18. </el-tree>
  19. </div>
  20. <div style="height: 60px;position: absolute;bottom: 0;right: 0;left: 0;" class="border d-flex align-items-center px-3 bg-white">
  21. <el-button @click="drawer = false">取消</el-button>
  22. <el-button type="primary" @click="submitRules">确定</el-button>
  23. </div>
  24. </div>
  25. </el-drawer>

9.0 element-ui promise自定义校验

  1. validator: async (rule, value)=>{
  2. // 请求判断用户是否存在
  3. let res = await that.certificate(value);
  4. if(res.status === 1)
  5. return Promise.reject('用户名已存在');
  6. else return Promise.resolve('用户名可用');
  7. },

10.0 反复点击清除背景颜色问题

  1. -webkit-user-select: none;
  2. -moz-user-select: none;
  3. -ms-user-select: none;
  4. user-select: none;

11.0 防抖函数处理请求

  1. this.debounce(this.getEmployeeQueryDepartmentUser, 600)
  1. debounce(fn, wait) {
  2. if (this.timer !== null) {
  3. clearTimeout(this.timer)
  4. }
  5. this.timer = setTimeout(fn, wait)
  6. }