vue实现流程页面
Vue 实现流程页面的方法
流程页面通常用于展示步骤、进度或工作流。以下是几种常见的实现方式:
使用时间轴组件
Element UI 和 Ant Design Vue 提供了现成的时间轴组件:
<template>
<el-timeline>
<el-timeline-item
v-for="(step, index) in steps"
:key="index"
:timestamp="step.time">
{{ step.content }}
</el-timeline-item>
</el-timeline>
</template>
<script>
export default {
data() {
return {
steps: [
{ time: '2023-01-01', content: '步骤1' },
{ time: '2023-01-02', content: '步骤2' }
]
}
}
}
</script>
自定义进度条
通过CSS和动态样式实现流程进度指示:
<template>
<div class="progress-container">
<div
v-for="(step, index) in steps"
:key="index"
:class="['step', { 'active': currentStep >= index }]">
<div class="step-circle">{{ index + 1 }}</div>
<div class="step-label">{{ step.label }}</div>
</div>
<div class="progress-line" :style="{ width: progressWidth }"></div>
</div>
</template>
<script>
export default {
props: ['currentStep'],
data() {
return {
steps: [
{ label: '提交申请' },
{ label: '审核中' },
{ label: '已完成' }
]
}
},
computed: {
progressWidth() {
return `${(this.currentStep / (this.steps.length - 1)) * 100}%`
}
}
}
</script>
<style>
.progress-container {
display: flex;
position: relative;
justify-content: space-between;
}
.progress-line {
position: absolute;
height: 2px;
background: #409EFF;
top: 15px;
left: 0;
z-index: 0;
transition: width 0.3s;
}
</style>
使用流程图库
对于复杂流程,可以集成专业库:
-
安装
vue-flow:npm install @braks/vue-flow -
基本用法:
<template> <VueFlow v-model="elements" :nodes="nodes" :edges="edges" @node-click="handleNodeClick" /> </template>
export default { components: { VueFlow }, data() { return { nodes: [ { id: '1', position: { x: 0, y: 0 }, data: { label: '节点1' } }, { id: '2', position: { x: 200, y: 0 }, data: { label: '节点2' } } ], edges: [ { id: 'e1-2', source: '1', target: '2' } ] } } }
```状态管理
对于多步骤表单流程,建议结合Vuex或Pinia:
// store.js
import { defineStore } from 'pinia'
export const useFlowStore = defineStore('flow', {
state: () => ({
currentStep: 0,
steps: ['info', 'confirm', 'complete']
}),
actions: {
nextStep() {
if (this.currentStep < this.steps.length - 1) {
this.currentStep++
}
}
}
})
路由分段
将不同步骤对应不同路由:
const routes = [
{ path: '/flow/step1', component: Step1 },
{ path: '/flow/step2', component: Step2 },
{ path: '/flow/step3', component: Step3 }
]
动画过渡
为步骤切换添加过渡效果:
<transition name="fade" mode="out-in">
<component :is="currentStepComponent" />
</transition>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
根据项目复杂度选择合适方案,简单流程可使用纯CSS实现,复杂业务流程建议结合状态管理和专业图表库。







