UI 组件的二次封装

UI 组件的二次封装是指,在基础 UI 库的组件上进行自定义封装,以实现更贴合业务需求的功能和样式。通过二次封装,可以增强组件的复用性、便捷性和一致性,简化业务代码,同时降低后续维护成本。

1. 二次封装的原理

二次封装基于对原有组件的拓展和定制,一般使用 组合 和 继承 的方式实现。具体实现时,可以利用基础 UI 组件库(如 Element Plus、Vant、Ant Design 等)中的现有组件,通过添加额外的属性、方法或样式,实现业务层面所需的定制功能。

核心思想包括:

1、组合和继承:基于已有组件,通过组合或继承来实现新的组件功能。

2、属性代理:将原有组件的属性透传到封装组件,以保持原组件的功能。

3、事件透传:将用户操作的事件传递给外部调用者,支持外部绑定事件处理。

4、样式定制:通过样式扩展或修改,实现特定样式需求。

5、业务逻辑注入:通过自定义逻辑封装特定的业务逻辑,比如权限控制、数据校验、异步加载等。

2. 原理解释

1、对属性和事件的传递

举个 🌰,对 Element-plus 的 el-input 进行处理。

在 App.vue 中:

html 复制代码
<template>
  <MyInput a="1" b="2" c="3" @change="() => {}" v-model="defaultValue" />
</template>

<script setup>
import MyInput from './components/MyInput.vue'
import { ref } from 'vue'
const defaultValue = ref('default value')
</script>

在 MyInput.vue 中:

html 复制代码
<template>
  <div class="my-input">
    <ElInput />
  </div>
</template>
<script>
import { ElInput } from 'element-plus';
export default {
  components:{
    ElInput
  },
  mounted() {
    console.log(this.$attrs)
  }
}
</script>

使用 $attrs 获取父组件传递的数据,展示为:

但当我们在 自定义组件 内部声明同名属性时:

html 复制代码
<template>
  <div class="my-input">
    <ElInput />
  </div>
</template>
<script>
import { ElInput } from 'element-plus'
export default {
  props: ['a'],
  components: {
    ElInput
  },
  mounted() {
    // this.$attrs 会自动剔除掉声明的属性,也就是在 props 内部定义的
    console.log(this.$attrs)
  }
}
</script>

展示如下:

这个现象是合理的,声明这个属性,说明我们接下来需要使用它做一些额外的处理,对组件本身是有意义的。而没有声明的数据,就汇总到 $attrs 中,可以将这些属性透传给 原始组件 本身。

如下:

html 复制代码
<template>
  <div class="my-input">
    <ElInput v-bind="$attrs"/>
  </div>
</template>
<script>
import { ElInput } from 'element-plus'
export default {
  props: ['a'],
  components: {
    ElInput
  },
  mounted() {
    console.log(this.$attrs)
  }
}
</script>

对此可以实现,将传递过来的、无需额外更改的属性和方法,透传到 UI 库组件本身。

2、对 插槽(slot)进行处理

在 App.vue 中,传递两个插槽内容。

html 复制代码
<template>
  <MyInput>
    <template #prepend>
      <el-button>prepend</el-button>
    </template>
    <template #append>
      <el-button>append</el-button>
    </template>
  </MyInput>
</template>

在 MyInput 组件中:

html 复制代码
<template>
  <div class="my-input">
    <ElInput v-bind="$attrs">
      <!-- 遍历传递给当前组件的所有插槽($slots),并生成每个插槽对应的模板。 -->
      <template v-for="(_, name) in $slots" #[name]="scopeValue">
        <slot :name="name" v-bind="scopeValue"></slot>
      </template>
    </ElInput>
  </div>
</template>

展示如下,父组件将插槽内容传递给子组件。

3、对 ref 的传递(!important)

虽然在某些特殊的场景下需要使用 ref,但是不建议对 ref 进行传递,那如何处理呢?

在父组件中获取子组件的对象,无非是使用其方法和属性,那就将这些属性传递出去。

App.vue

html 复制代码
<template>
  <MyInput ref="myInput"></MyInput>
</template>

<script setup>
import MyInput from './components/MyInput.vue'
import { onMounted, ref } from 'vue'
const myInput = ref(null)
onMounted(() => {
  console.log('~ myInput:', myInput.value)
  myInput.value.focus && myInput.value.focus()
})
</script>

MyInput.vue

html 复制代码
<template>
  <div class="my-input">
    <el-input ref="input" />
  </div>
</template>
<script>
import { ElInput } from 'element-plus'
export default {
  components: {
    ElInput
  },
  mounted() {
    console.log('refs', this.$refs.input)
    for (let key in this.$refs.input) {
      // 将组件 input 上的方法,添加到该组件本身,也就是 MyInput 组件
      this[key] = this.$refs.input[key]
    }
  }
}
</script>

在 App.vue 中调用子组件的 focus 方法:

以此实现,父组件调用子组件的方法。

相关推荐
Z兽兽26 分钟前
React@18+Vite项目配置env文件
前端·react.js·前端框架
SuniaWang33 分钟前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
A_nanda1 小时前
根据AI提示排查vue前端项目
前端·javascript·vue.js
happymaker06262 小时前
web前端学习日记——DAY05(定位、浮动、视频音频播放)
前端·学习·音视频
~无忧花开~2 小时前
React状态管理完全指南
开发语言·前端·javascript·react.js·前端框架
LegendNoTitle2 小时前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
@大迁世界2 小时前
1.什么是 ReactJS?
前端·javascript·react.js·前端框架·ecmascript
BJ-Giser3 小时前
Cesium 基于EZ-Tree的植被效果
前端·可视化·cesium
王码码20354 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
发现一只大呆瓜4 小时前
深入浅出 AST:解密 Vite、Babel编译的底层“黑盒”
前端·面试·vite