组件封装 - steps组件

article/2025/9/17 22:11:28

首先, 我先来看看效果

steps 组件的封装和 tabs 组件还是相似的

都会去指定两个组件来完成(仿Element UI), 都会去使用 jsx 的语法

让其中一个组件去规定样式和排版, 另外一个组件去接收父组件传入的动态数据

但和面包屑组件还是有区别的(面包屑组件封装):

相同点都是使用两个组件来完成(一个指定排版, 另外一个指定内容), 都是使用 render 函数来进行渲染

不同点是, bread 组件使用的是 h 函数来创建虚拟节点; 然后使用 render 来进行渲染

而 tabs 和 steps 使用的是 jsx 语法来指定标签(使用 jsx 的原因是, 需要指定两个以上的标签且有嵌套关系, 如果使用 h 函数的话太麻烦了)

第一步: 

1. 创建 steps 和 steps-item 组件

2. steps-item 组件接收 title 和 desc 数据

<template><div class="steps"><div class="steps-item active" v-for="i in 5" :key="i"><div class="step"><span>{{i}}</span></div><div class="title">提交订单</div><div class="desc">2021-03-18 02:11:47</div></div></div>
</template><script>
export default {name: 'Steps'
}
</script><style lang="less">
.steps {display: flex;text-align: center;&-item {flex: 1;&:first-child {.step {&::before {display: none;}}}&:last-child {.step {&::after {display: none;}}}&.active {.step {> span {border-color: @xtxColor;background: @xtxColor;color: #fff}&::before,&::after {background: @xtxColor;}}.title {color: @xtxColor;}}.step {position: relative;> span {width: 48px;height: 48px;font-size: 28px;border: 2px solid #e4e4e4;background: #fff;border-radius: 50%;line-height: 44px;color: #ccc;display: inline-block;position: relative;z-index: 1;}&::after,&::before{content: "";position: absolute;top: 23px;width: 50%;height: 2px;background: #e4e4e4;}&::before {left: 0;}&::after {right: 0;}}.title {color: #999;padding-top: 12px;}.desc {font-size: 12px;color: #999;padding-top: 6px;}}
}
</style>

steps-item 主要是去接收外部传入的 title 和 desc 的动态数据, 然后将这些数据直接给到 steps 来进行出来

所以不需要使用 <slot /> 

<script>
export default {name: 'StepsItem',props: {title: {type: String,default: ''},desc: {type: String,default: ''}}
}
</script>

第二步:

1. 显示 steps 组件数据

也就是在 steps 组件中接收到一个 active 的数据(父组件发送请求获取过来的节点数据)

首先需要拿到所有的 steps-item 组件数据(通过 $slots.default)

然后, 判断 $slots.default 返回的数据中对象中的 type 数据是否为代码片段(这里的详细讲解在 tabs 组件封装时讲过了(tabs 组件封装)

然后指定显示内容

<script>
import { getCurrentInstance } from 'vue'
export default {name: 'Steps',render () {// 这是在proxy的作用相当于vue2中的this// 这里其实也可以直接使用this, 因为不是在setup中; 都无所谓的const { proxy } = getCurrentInstance()const items = proxy.$slots.default()const dynamicItems = []items.forEach(item => {if (item.type.name === 'StepsItem') {dynamicItems.push(item)} else {item.children.forEach(c => {dynamicItems.push(c)})}})const itemsJsx = dynamicItems.map((item, i) => {return <div class="steps-item"><div class="step"><span>{i + 1}</span></div><div class="title">{item.props.title}</div><div class="desc">{item.props.desc}</div></div>})return <div class="steps">{itemsJsx}</div>}
}
</script><style lang="less">
.steps {display: flex;text-align: center;&-item {flex: 1;&:first-child {.step {&::before {display: none;}}}&:last-child {.step {&::after {display: none;}}}&.active {.step {> span {border-color: @xtxColor;background: @xtxColor;color: #fff}&::before,&::after {background: @xtxColor;}}.title {color: @xtxColor;}}.step {position: relative;> span {width: 48px;height: 48px;font-size: 28px;border: 2px solid #e4e4e4;background: #fff;border-radius: 50%;line-height: 44px;color: #ccc;display: inline-block;position: relative;z-index: 1;}&::after,&::before{content: "";position: absolute;top: 23px;width: 50%;height: 2px;background: #e4e4e4;}&::before {left: 0;}&::after {right: 0;}}.title {color: #999;padding-top: 12px;}.desc {font-size: 12px;color: #999;padding-top: 6px;}}
}
</style>

第三步:

1. 在 steps 组件中接收 active 数据, 然后根据 active 数据动态显示 steps 的节点

<script>
import { getCurrentInstance } from 'vue'
export default {name: 'Steps',props: {active: {type: Number,default: 1}},render () {// 这是在proxy的作用相当于vue2中的this// 这里其实也可以直接使用this, 因为不是在setup中; 都无所谓的const { proxy } = getCurrentInstance()const items = proxy.$slots.default()const dynamicItems = []items.forEach(item => {if (item.type.name === 'StepsItem') {dynamicItems.push(item)} else {item.children.forEach(c => {dynamicItems.push(c)})}})const itemsJsx = dynamicItems.map((item, i) => {// 只要是下标值i小于active的div标签都会添加上active类return <div class="xtx-steps-item" class={{ active: i < props.active }}><div class="step"><span>{i + 1}</span></div><div class="title">{item.props.title}</div><div class="desc">{item.props.desc}</div></div>})return <div class="steps">{itemsJsx}</div>}
}
</script><style lang="less">
.steps {display: flex;text-align: center;&-item {flex: 1;&:first-child {.step {&::before {display: none;}}}&:last-child {.step {&::after {display: none;}}}&.active {.step {> span {border-color: @xtxColor;background: @xtxColor;color: #fff}&::before,&::after {background: @xtxColor;}}.title {color: @xtxColor;}}.step {position: relative;> span {width: 48px;height: 48px;font-size: 28px;border: 2px solid #e4e4e4;background: #fff;border-radius: 50%;line-height: 44px;color: #ccc;display: inline-block;position: relative;z-index: 1;}&::after,&::before{content: "";position: absolute;top: 23px;width: 50%;height: 2px;background: #e4e4e4;}&::before {left: 0;}&::after {right: 0;}}.title {color: #999;padding-top: 12px;}.desc {font-size: 12px;color: #999;padding-top: 6px;}}
}
</style>

父组件中使用情况

根据后端的数据来决定 steps 的节点数据

<template><div class="detail-steps"><Steps :active="order.orderState===6 ? 1: order.orderState"><StepsItem title="提交订单" :desc="order.createTime" /><StepsItem title="付款成功" :desc="order.payTime" /><StepsItem title="商品发货" :desc="order.consignTime" /><StepsItem title="确认收货" :desc="order.evaluationTime" /><StepsItem title="订单完成" :desc="order.endTime" /></Steps></div>
</template>


http://chatgpt.dhexx.cn/article/WGKadwH7.shtml

相关文章

ElementUi Steps 步骤条的使用

效果&#xff1a; 实现&#xff1a; <el-steps :active"active" finish-status"success"><el-step title"步骤 1"></el-step><el-step title"步骤 2"></el-step><el-step title"步骤 3"&…

el-steps(步骤条)的入门学习

el-steps(步骤条)的入门学习 适用场景 在有步骤流程的情况下&#xff0c;可以设置步骤条&#xff0c;引导用户向下操作&#xff0c;如四六级的报考 知识点 el-steps嵌套el-step使用el-steps的active设置Number&#xff0c;从零开始el-steps的space设置Number&#xff0c;为…

Vue2步骤条(Steps)

Vue3步骤条&#xff08;Steps&#xff09; 可自定义设置以下属性&#xff1a; 步骤标题数组&#xff08;stepsLabel&#xff09;步骤描述数组&#xff08;stepsDesc&#xff09;步骤总数&#xff08;totalSteps&#xff09;&#xff0c;默认3当前选中的步骤&#xff08;curren…

Vue3步骤条(Steps)

Vue2步骤条&#xff08;Steps&#xff09; 可自定义设置以下属性&#xff1a; 步骤数组&#xff08;steps&#xff09;&#xff0c;类型&#xff1a;Array<{title?: string, description?: string}>&#xff0c;默认 [] 当前选中的步骤&#xff0c;设置 v-model 后&a…

CSS3动画——steps函数

CSS3动画中应用的定时函数&#xff08;timing function&#xff09;常用的有内置关键字linear, ease, ease-in, ease-out, ease-in-out, step-start, step-end&#xff0c;另外还有两个内置的缓动函数cubic-bezier(), steps() 其中steps()这个函数是将整个动画过程分为指定的步…

css3中animation的steps分步动画

css的animation中&#xff0c;有一种描述动画变化速率的东西&#xff0c;例如常见的linear,ease-in,ease-out等&#xff0c;这些都是连续的变化&#xff0c;还有一种叫做steps的&#xff0c;它用来描述一种不连续的动画&#xff0c;也就是逐帧动画。 基本认识 steps的格式为&…

超几何分布(Hypergeometric distribution)

超几何分布 百度解释 https://baike.baidu.com/item/超几何分布/4782968?fraladdin 通俗解释 超几何分布中的参数是M,N,n&#xff0c;超几何分布记作X~H(N,M,n) 。假如一共有100颗球&#xff0c;20颗为红球&#xff0c;80颗为白球&#xff0c;从中不放回地拿出10个球&#x…

机器学习小组知识点12:超几何分布(Hyper-Geometric Distribution)

超几何分布与二项分布的联系与区别 事实上,超几何分布和二项分布确实有着密切的联系&#xff0c;但也有明显的区别。   课本对于超几何分布的定义是这样的&#xff1a;一般的&#xff0c;若一个随机变量X的分布列为&#xff0c;其中&#xff0c;则称X服从超几何分布&#xf…

常见分布总结-高斯分布、伯努利分布、泊松分布、几何分布、beta分布

概率分布 概率分布是指用于表述随机变量取值的概率规律,包括连续分布和离散分布。 下面作了这些概率分布的一个思维导图。 文章目录 概率分布1、离散概率分布1.1、两点分布2.2、 二项分布1.3、几何分布1.4、超几何分布1.5、泊松分布2、连续概率分布2.1、均匀分布2.2、正太分布…

超几何分布的其他形式及其解释?

超几何分布定义 百度百科&#xff1a;超几何分布是统计学上一种离散概率分布。它描述了从有限N个物件&#xff08;其中包含M个指定种类的物件&#xff09;中抽出n个物件&#xff0c;成功抽出该指定种类的物件的次数&#xff08;不放回&#xff09;。称为超几何分布&#xff0c;…

geometric distribution and exponential distribution(几何分布和指数分布)

几何分布 分布函数均值和方差意义 表示经过k次实验才第一次得到正确的实验结果 比如抛硬币得到正面的需要抛的次数 指数分布 分布函数均值和方差意义 表示经过一段x之后&#xff0c;某件事第一次发生 比如经过x时间之后&#xff0c;公交车来的概率 比如餐厅从开业到第一个客人…

概率论的学习和整理9:超几何分布 (未完成!!!)

目录 1超几何分布 Hypergeometric distribution 1.1 超几何分布的定义 1.2 为什么叫超几何分布 1.3 超几何分布的公式 &#xff08;2种公式&#xff09; 1.3.1 超几何分布的公式1 &#xff08;总体型公式&#xff09; 1.3.2 超几何分布的公式2 &#xff08;拆…

概率论与数理统计基础(二):常用离散分布 二项、泊松、超几何分、几何、 负二项分布

本文列举了常见的离散分布&#xff0c;关于它们的背景、概率分布列、数学期望与方差&#xff0c;以及与之相关的一些重要性质&#xff1b;比如几何分布的无记忆性、 二项分布的泊松近似、超几何分布的二项近似。。。。可作为离散分布的知识速查表。 目录 1. 二项分布b(n,p) …

如何理解几何分布与指数分布的无记忆性?

在经济学上&#xff0c;有一个概念是沉没成本&#xff0c;大概指的是已经付出的、且不可收回的成本。针对这个概念有一个常见的说法&#xff1a; 这句话的意思是&#xff0c;既然沉没成本不可收回&#xff0c;那么在做选择的时候就不应该考虑它。举一个简单的例子&#xff0c;买…

几何分布的期望公式的推导

随机变量服从几何分布 概率分布 期望 现在先求等差比数列和 ②-③, 并运用等比数列求和公式,可得 将④代入①得

统计学:几何分布、二项分布、泊松分布

一、几何分布 假设某种赌博游戏的胜率为0.2&#xff0c;那么意味着你玩第一次就胜出的概率为0.2。 那玩第二次才胜出呢&#xff1f;“玩第二次才胜出”就意味着玩第一次是失败的&#xff0c;而直到第二次才胜出&#xff0c;那么这件事发生的概率就是0.80.20.16。 那么第三次…

统计学 分布篇 - Hypergeometric Distribution(超几何分布)

超几何分布: 是 离散随机分布的一种. 它描述的是 从 n 中 拿 k 个成功的事件的概率( 不放回, 不放回意味着该事件是非独立事件), 其中在 N 中一共有 K 个成功事件. n 为 样本数量, k 为样本中成功的概率 N为 事件的总数量(population), K为 在N中 事件的总数量. note: 超几…

几何分布的期望与方差

几何分布的期望与方差 高中数学教科书新版第三册&#xff08;选修II&#xff09;比原来的修订本新增加随机变量的几何分布&#xff0c;但书中只给出了结论&#xff1a;&#xff08;1&#xff09;&#xff0c;&#xff08;2&#xff09;&#xff0c;而未加以证明。本文给出证明&…

概率统计14——几何分布

我家小朋友年方1岁半&#xff0c;家里每天上午都要出去遛小孩。现在小朋友有两项爱好&#xff0c;在家翻垃圾桶&#xff0c;出门捡烟头。 翻垃圾桶可以有效地限制&#xff0c;捡烟头可是防不胜防。 也许烟头能散发出特殊的能量波动&#xff0c;小区的绿化带和草坪上的大部分烟…