前端项目公共组件封装思想(Vue)

1. 通用组件(表单搜索+表格展示+分页器)

在项目当中我们总会遇到这样的页面:页面顶部是一个表单筛选项,下面是一个表格展示数据。表格下方是一个分页器,这样的页面在我们的后台管理系统中经常所遇到,有时候可能不止一个页面,好几个页面的结构都是这种。如图:

本人记得,在react中的高级组件库中有这么一个组件,就实现了这么一个效果。就拿这个页面来说我们实现一下组件封装的思想:1.首先把每个页面的公共部分抽出来,比如标题等,用props或者插槽的形式传入到组件中进行展示 2. 可以里面数据的双向绑定实现跟新的效果 3. 设置自定义函数传递给父组件要做上面事情

1.将公共的部分抽离出来

js 复制代码
TableContainer组件
<template>
    <div class="container">
      <slot name="navbar"></slot>
      <div class="box-detail">
        <div class="detail-box">
          <div class="box-left">
            <div class="left-bottom">
              <div class="title-bottom">{{ title }}</div>
              <div class="note">  
                <div class="note-detail">
                  <slot name="table"></slot>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <el-backtop style="width: 3.75rem; height: 3.75rem" :bottom="10" :right="5">
        <div
          style="
             {
              width: 5.75rem;
              flex-shrink: 0;
              border-radius: 2.38rem;
              background: #fff;
              box-shadow: 0 0.19rem 1rem 0 #2b4aff14;
            }
          "
        >
          <i class="el-icon-arrow-up" style="color: #6e6f74"></i>
        </div>
      </el-backtop>
    </div>
  </template>

这里的话利用了具名插槽插入了navbar、table组件,title通过props的属性传入到子组件当中。进行展示,

js 复制代码
父组件
    <TableContainer title="资源审核">
        <template v-slot:navbar>
            <my-affix :offset="0">
                <Navbar/>
            </my-affix>
        </template>
        <template v-slot:table>
            <SourceAuditTable/>
        </template>
    </TableContainer>

当然这是一个非常非常简单的组件封装案例

接下来我们看一个高级一点的组件封装

父组件

js 复制代码
<template>
  <div>
    <hr>
    <HelloWorld :page.sync="page" :limit.sync="limit" />
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
  data() {
    return {
      page: 1,
      limit: 5
    }
  },
  components: {
    HelloWorld
  },


}
</script>

父组件传递给子组件各种必要的属性:total(总共多少条数据)、page(当前多少页)、limit(每页多少条数据)、pageSizes(选择每页大小数组)

子组件

js 复制代码
<template>
  <el-pagination :current-page.sync="currentPage" :page-size.sync="pageSize" :total="20" />
</template>

<script>
export default {
  name: 'HelloWorld',

  props: {
    page: {
      default: 1
    },
    limit: {
      default: 5
    },
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        //currentPage 这里对currentPage做出来改变就会走这里
        //这边更新数据走这里
        console.log('currentPage', this.currentPage)
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

这里的page.sync、limit.sync目的就是为了实现数据的双向绑定,computed中监听page和limit的变化,子组件接收的数据通过computed生成的currentPage通过sync绑定到了 el-pagination中, 点击分页器的时候会改变currentPage 此时会调用set函数设置新的值,通过代码 this.$emit(update:page,value) 更新父组件中的值,实现双向的数据绑定

本文是作者在闲暇的时间随便记录一下, 若有错误请指正,多多包涵。感谢支持!

相关推荐
Hyyy10 小时前
普通前端续命周报——第1周
前端·javascript
KaMeidebaby10 小时前
卡梅德生物技术快报|抗独特型抗体开发:半抗原检测技术瓶颈拆解,抗独特型抗体开发工程化实践
前端·数据库·人工智能·其他·百度·新浪微博
2501_9400417410 小时前
纯前端创意交互:五款全新实用工具与视觉应用生成指南
前端·交互
刀法如飞10 小时前
《道德经》简单解说版-第 2 章:天下皆知美之为美
前端·后端·面试
发现一只大呆瓜12 小时前
超全 Vite 性能优化指南:网络、资源、预渲染三维落地方案
前端·面试·vite
IT_陈寒13 小时前
Vue的computed属性怎么突然不更新了?
前端·人工智能·后端
智商不够_熬夜来凑13 小时前
【Picker】单选多选
前端·javascript·vue.js
米饭不加菜13 小时前
Typora 原生流程图语法完全指南(Flowchart.js)
前端·javascript·流程图
scan72414 小时前
langgraphy条件边
前端·javascript·html
冰小忆14 小时前
类变量在继承场景下的初始化规则是怎样的?
java·前端·数据库