vue实现steps步骤条
Vue 实现 Steps 步骤条
使用 Element UI 的 Steps 组件
安装 Element UI 库:
npm install element-ui
在 Vue 文件中引入 Steps 组件:
<template>
<div>
<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>
<el-button @click="next">下一步</el-button>
</div>
</template>
<script>
export default {
data() {
return {
active: 0
}
},
methods: {
next() {
if (this.active++ > 2) this.active = 0
}
}
}
</script>
使用 Ant Design Vue 的 Steps 组件
安装 Ant Design Vue 库:
npm install ant-design-vue
在 Vue 文件中引入 Steps 组件:
<template>
<div>
<a-steps :current="current">
<a-step title="步骤 1" />
<a-step title="步骤 2" />
<a-step title="步骤 3" />
</a-steps>
<a-button type="primary" @click="next">下一步</a-button>
</div>
</template>
<script>
export default {
data() {
return {
current: 0
}
},
methods: {
next() {
if (this.current++ > 2) this.current = 0
}
}
}
</script>
自定义 Steps 组件
创建一个自定义 Steps 组件:
<template>
<div class="steps">
<div
v-for="(step, index) in steps"
:key="index"
class="step"
:class="{ active: currentStep === index, completed: currentStep > index }"
>
<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;
margin-bottom: 20px;
}
.step {
flex: 1;
text-align: center;
position: relative;
}
.step-circle {
width: 30px;
height: 30px;
border-radius: 50%;
background: #ccc;
color: white;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 10px;
}
.step.active .step-circle {
background: #409eff;
}
.step.completed .step-circle {
background: #67c23a;
}
.step-title {
color: #999;
}
.step.active .step-title {
color: #409eff;
}
.step.completed .step-title {
color: #67c23a;
}
</style>
在父组件中使用自定义 Steps 组件:
<template>
<div>
<custom-steps :steps="steps" :current-step="currentStep" />
<button @click="nextStep">下一步</button>
</div>
</template>
<script>
import CustomSteps from './CustomSteps.vue'
export default {
components: {
CustomSteps
},
data() {
return {
steps: [
{ title: '第一步' },
{ title: '第二步' },
{ title: '第三步' }
],
currentStep: 0
}
},
methods: {
nextStep() {
if (this.currentStep < this.steps.length - 1) {
this.currentStep++
}
}
}
}
</script>






