vue实现步骤条
Vue 实现步骤条的方法
使用第三方库(如 Element UI)
Element UI 提供了现成的 Steps 组件,可以快速实现步骤条功能。安装 Element UI 后,直接使用 el-steps 组件即可。
<template>
<el-steps :active="active" finish-status="success">
<el-step title="步骤 1"></el-step>
<el-step title="步骤 2"></el-step>
<el-step title="步骤 3"></el-step>
</el-steps>
</template>
<script>
export default {
data() {
return {
active: 0
};
}
};
</script>
通过修改 active 的值可以控制当前步骤的进度。
自定义步骤条组件
如果需要更灵活的样式或功能,可以自定义步骤条组件。以下是一个简单的实现示例:
<template>
<div class="steps">
<div
v-for="(step, index) in steps"
:key="index"
class="step"
:class="{ active: index === currentStep, completed: index < currentStep }"
>
<div class="step-circle">{{ index + 1 }}</div>
<div class="step-title">{{ step.title }}</div>
</div>
</div>
</template>
<script>
export default {
props: {
steps: {
type: Array,
required: true
},
currentStep: {
type: Number,
default: 0
}
}
};
</script>
<style>
.steps {
display: flex;
justify-content: space-between;
}
.step {
text-align: center;
flex: 1;
position: relative;
}
.step-circle {
width: 30px;
height: 30px;
border-radius: 50%;
background: #ccc;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.step.active .step-circle {
background: #409eff;
color: white;
}
.step.completed .step-circle {
background: #67c23a;
color: white;
}
</style>
动态控制步骤进度
通过绑定数据和事件,可以动态控制步骤条的进度。例如,结合按钮切换步骤:
<template>
<div>
<custom-steps :steps="steps" :currentStep="currentStep" />
<button @click="prevStep" :disabled="currentStep === 0">上一步</button>
<button @click="nextStep" :disabled="currentStep === steps.length - 1">下一步</button>
</div>
</template>
<script>
import CustomSteps from './CustomSteps.vue';
export default {
components: {
CustomSteps
},
data() {
return {
steps: [
{ title: '填写信息' },
{ title: '验证身份' },
{ title: '完成注册' }
],
currentStep: 0
};
},
methods: {
prevStep() {
if (this.currentStep > 0) this.currentStep--;
},
nextStep() {
if (this.currentStep < this.steps.length - 1) this.currentStep++;
}
}
};
</script>
结合路由实现步骤导航
在多页表单或向导场景中,可以将步骤条与 Vue Router 结合,通过路由切换步骤:
// 路由配置
const routes = [
{ path: '/step1', component: Step1 },
{ path: '/step2', component: Step2 },
{ path: '/step3', component: Step3 }
];
在步骤条组件中,通过 $route.path 判断当前步骤并高亮显示。

以上方法可以根据实际需求选择或组合使用,实现灵活多样的步骤条功能。






