Implementing pagination in a Vue.js application involves creating a component to handle the pagination logic and then integrating it into the main application. Additionally, you mentioned "must-clickable links," so I'll assume you want links to specific pages.
Here's a basic example of how you can implement pagination with clickable links in a Vue.js application:
Create a Pagination Component:
html<!-- Pagination.vue -->
<template>
<div class="pagination">
<span v-if="currentPage > 1" @click="changePage(currentPage - 1)">Previous</span>
<span v-for="page in pages" :key="page" @click="changePage(page)" :class="{ active: page === currentPage }">{{ page }}</span>
<span v-if="currentPage < totalPages" @click="changePage(currentPage + 1)">Next</span>
</div>
</template>
<script>
export default {
props: {
currentPage: Number,
totalPages: Number,
},
data() {
return {
pages: [],
};
},
watch: {
currentPage() {
this.calculatePages();
},
totalPages() {
this.calculatePages();
},
},
mounted() {
this.calculatePages();
},
methods: {
calculatePages() {
this.pages = [];
for (let i = 1; i <= this.totalPages; i++) {
this.pages.push(i);
}
},
changePage(page) {
// Emit an event to notify the parent component about the page change
this.$emit('page-change', page);
},
},
};
</script>
<style scoped>
.pagination {
display: flex;
justify-content: center;
margin-top: 10px;
}
.pagination span {
margin: 0 5px;
cursor: pointer;
}
.pagination .active {
font-weight: bold;
}
</style>
Use the Pagination Component in Your Main Component:
html<!-- MainComponent.vue -->
<template>
<div>
<!-- Your main content here -->
<!-- Include the Pagination component -->
<pagination :currentPage="currentPage" :totalPages="totalPages" @page-change="handlePageChange" />
</div>
</template>
<script>
import Pagination from './Pagination.vue';
export default {
components: {
Pagination,
},
data() {
return {
currentPage: 1,
itemsPerPage: 10,
totalItems: 100,
};
},
computed: {
totalPages() {
return Math.ceil(this.totalItems / this.itemsPerPage);
},
},
methods: {
handlePageChange(page) {
// Handle page change logic (e.g., fetch data for the new page)
// For now, just update the current page
this.currentPage = page;
},
},
};
</script>
This example assumes that you have a total number of items (totalItems
) and a specified number of items per page (itemsPerPage
). Adjust the values and logic based on your specific requirements. The handlePageChange
method can be used to fetch new data or perform any action when the page changes.