Implementing a progress tracker in Vue.js involves creating a component that displays the progress of a multi-step process. Here's a simple example to get you started:
Create a new Vue component:
html<!-- ProgressTracker.vue -->
<template>
<div class="progress-tracker">
<div v-for="(step, index) in steps" :key="index" :class="{ 'completed': index < currentStep }">
{{ step }}
</div>
</div>
</template>
<script>
export default {
props: {
steps: {
type: Array,
required: true,
},
currentStep: {
type: Number,
required: true,
},
},
};
</script>
<style scoped>
.progress-tracker {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.progress-tracker div {
flex: 1;
text-align: center;
padding: 10px;
background-color: #e0e0e0;
border-radius: 5px;
}
.progress-tracker div.completed {
background-color: #4caf50; /* Green */
color: white;
}
</style>
This component accepts two props: steps
(an array of strings representing each step) and currentStep
(the current step in the process).
Use the ProgressTracker component in your parent component:
html<!-- App.vue -->
<template>
<div id="app">
<progress-tracker :steps="steps" :currentStep="currentStep" />
<!-- Your other content goes here -->
</div>
</template>
<script>
import ProgressTracker from "./components/ProgressTracker.vue";
export default {
components: {
ProgressTracker,
},
data() {
return {
steps: ["Step 1", "Step 2", "Step 3", "Step 4"],
currentStep: 2, // Set the current step dynamically based on your application state
};
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
This is a basic example, and you may need to customize it based on your specific requirements. Adjust the styling and structure as needed for your application.
Run your Vue.js application:
Make sure you have Vue CLI installed. If not, you can install it using npm install -g @vue/cli
. Then, run your application with npm run serve
.
Now, when you update the currentStep
in your parent component, the progress tracker will visually show the completed steps.