**子组件**
<template>
<div class="steps-box">
<div
v-for="(item, index) in stepsData"
class="steps-card"
:class="index <= stepsIndex ? 'on' : ''"
:key="index">
<span>{{ index + 1 }}</span>
<span>{{ item.name }}</span>
</div>
</div>
</template>
``
```javascript
<script>
export default {
data() {
return {
stepsIndex: "",
stepsData: [],
};
},
created() {
this.stepsIndex = this.$parent.stepsIndex;
this.stepsData = this.$parent.stepsData;
},
methods: {
stepsFun() {
this.stepsIndex = this.$parent.stepsIndex;
}
},
};
</script>
<style lang="scss" scoped>
.steps-box {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 30px;
margin-top: 200px;
.steps-card {
width: 300px;
cursor: pointer;
display: flex;
align-items: center;
color: rgba(0, 0, 0, 0.25);
position: relative;
span:nth-of-type(1) {
display: inline-block;
width: 26px;
height: 26px;
text-align: center;
line-height: 26px;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 50%;
margin-right: 10px;
i {
font-style: normal;
}
}
}
.steps-card:before {
width: 135px;
height: 1px;
content: "";
background: #e9e9e9;
margin: 0 15px;
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 0;
}
.steps-card:last-of-type {
width: auto;
}
.steps-card:last-of-type:before {
width: 0;
}
.steps-card.on {
display: flex;
align-items: center;
color: rgba(0, 0, 0, 0.85);
span:nth-of-type(1) {
display: inline-block;
width: 26px;
height: 26px;
text-align: center;
line-height: 26px;
background: #1d73f6;
border: 1px solid #1d73f6;
color: #fff;
border-radius: 50%;
margin-right: 10px;
}
}
.steps-card.on:before {
background: #1d73f6;
}
}
</style>
**父组件调用组件**
<div class="addTask">
<steps ref="stepsChild"></steps>
<div class="task-main-box">
<div v-if="stepsIndex == 0" class="task-con-box">
<h1>第一步</h1>
</div>
<div v-if="stepsIndex == 1" class="task-con-box">
<h1>第二步</h1>
</div>
<div v-if="stepsIndex == 2" class="task-con-box">
<h1>第三步</h1>
</div>
<div v-if="stepsIndex == 3" class="task-con-box">
<h1>第四步</h1>
</div>
</div>
<div class="button-box">
<div v-if="stepsIndex == 0">取消</div>
<div v-if="stepsIndex == 3">完成</div>
<div @click="prevSteps" v-if="stepsIndex != 0">上一步</div>
<div @click="nextSteps" v-if="stepsIndex != 3">下一步</div>
</div>
</div>
<script>
import steps from "./step.vue";
export default {
data() {
return {
stepsIndex: 0,
stepsData: [{
name: '第一步'
},
{
name: '第二步'
},
{
name: '第三步'
},
{
name: '第四步'
}
]
}
}
components: {
steps,
},
methods: {
prevSteps() {
this.stepsIndex--
this.$refs.stepsChild.stepsFun()
},
nextSteps() {
this.stepsIndex++
this.$refs.stepsChild.stepsFun()
}
}
}
</script>
<style lang="scss" scoped>
.addTask {
width: 100%;
height: 100%;
overflow: hidden;
.task-main-box {
width: 100%;
height: calc(100% - 45vh);
.task-con-box {
width: 100%;
height: 100%;
}
}
.button-box {
display: flex;
justify-content: center;
div {
padding: 10px;
background: #2F86FF;
margin: 10px;
border-radius: 4px;
cursor: pointer;
color: #ffffff;
}
}
}
</style>